Skip to content

Instantly share code, notes, and snippets.

View doug2k1's full-sized avatar

Douglas Matoso doug2k1

  • Campinas - SP - Brazil
View GitHub Profile
@doug2k1
doug2k1 / index.js
Last active October 2, 2017 12:19
Arduino light sensor example
const five = require('johnny-five')
const board = new five.Board()
board.on('ready', () => {
console.log('ready!')
const led = new five.Led(11)
const photoResistor = new five.Sensor({
pin: 'A2',
freq: 100
const fse = require('fs-extra')
const path = require('path')
const { promisify } = require('util')
const ejsRenderFile = promisify(require('ejs').renderFile)
const globP = promisify(require('glob'))
const config = require('../site.config')
const srcPath = './src'
const distPath = './public'
@doug2k1
doug2k1 / arrow.js
Created September 6, 2017 13:56
'this' no JavaScript: arrow function - https://blog.dmatoso.com/javascript-this-71dd763aad52
const player = {
name: 'Cloud',
weapon: 'Fusion Sword',
sayNameAndAttack () {
console.log(this.name)
// Uma 'arrow function'!
// O 'this' dentro dela vai ser o mesmo que aqui fora
window.setTimeout(() => {
console.log(`${this.name} ataca com a ${this.weapon}`)
// Usando 'class' o 'new' chama o construtor e faz a mesma coisa com o 'this'
class Player {
constructor (name) {
// const this = {}
this.name = name
this.hp = 1000
this.mp = 0
// return this
}
}
// As linhas comentadas mostram o que o 'new' faz pra gente
const Player = function (name) {
// const this = {}
this.name = name
this.hp = 1000
this.mp = 0
// return this
}
// Função chamada com 'new', portanto o 'this' será
const player = {
name: 'Cloud',
hp: 100,
mp: 0,
printStatus () {
console.log(`${this.name} tem ${this.hp} de HP e ${this.mp} de MP.`)
}
}
// Função usa 'this', mas vamos setá-lo no momento da chamada.
const player = {
name: 'Cloud'
}
const enemy = {
name: 'Sephirot'
}
// Função declarada global.
// Se chamar direto, o 'this' seria 'global' ou 'window'.
// Constante no objeto 'window'
window.playerName = 'Tidus'
const sayGlobalName = function () {
console.log(this.playerName)
}
// A chamada abaixo não se enquadra a nenhuma das outras regras,
// portanto o 'this' na função é o objeto global
// ('window' no navegador ou 'global' no Node.js)
const player = {
name: 'Cloud',
sayName () {
// Que valor 'this' tem aqui?
// Temos que olhar o momento que a função é chamada.
console.log(this.name)
}
}
// Dentro da função 'sayName' o 'this' é o que está antes do ponto.
// requisição:
// tenta fazer um GET em URI inexistente
fetch('https://api.github.com/nonexistent-uri')
.then(response => {
if (response.ok) {
// não deve entrar aqui
console.log('success')
} else {
console.log(response.status)
}