This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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á |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const player = { | |
name: 'Cloud' | |
} | |
const enemy = { | |
name: 'Sephirot' | |
} | |
// Função declarada global. | |
// Se chamar direto, o 'this' seria 'global' ou 'window'. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |