Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / object-method-scope.js
Created March 6, 2021 17:59
Example showing scope in traditional function object methods
const pokemon = {
experience: 0,
fight: function() {
console.log(this.experience);
this.experience += Math.floor(Math.random() * 10) + 1;
}
};
@NyaGarcia
NyaGarcia / object-method-scope-arrow.js
Last active March 6, 2021 17:59
Example showing scope in arrow function object methods
const pokemon = {
experience: 0,
fight: () => {
this.experience += Math.floor(Math.random() * 10) + 1;
}
};
pokemon.fight();
console.log(pokemon.experience);
// Output: 0
@NyaGarcia
NyaGarcia / arrow-function-destructuring.js
Created March 5, 2021 22:15
Destructuring arguments in arrow function
const logUser = ({ name, pass }) =>
`Logging user ${name} with password: ${pass}`;
const user = { name: "Nya", pass: "secretpassw0rd" };
console.log(logUser(user));
// Output: Logging user Nya with password: secretpassw0rd
@NyaGarcia
NyaGarcia / arrow-function-default-params.js
Last active March 5, 2021 22:14
Setting default parameters in an arrow function
const choosePokemon = (
firstPokemon = "Charmander",
secondPokemon = "Bulbasaur"
) => `I choose ${firstPokemon} and ${secondPokemon}`;
// Calling function without parameters
console.log(choosePokemon());
// Output: I choose Charmander and Bulbasaur
@NyaGarcia
NyaGarcia / prototype-comparison.js
Created March 5, 2021 15:47
Comparison between arrow and traditional function prototypes
function sayHello(name) {
return `Hello ${name}!`;
}
console.log(sayHello.prototype);
// Output: {...}
const sayHelloArrow = name => `Hello ${name}`;
console.log(sayHelloArrow.prototype);
// Output: undefined
@NyaGarcia
NyaGarcia / arrow-function-multiple-params.js
Created March 5, 2021 15:01
Arrow function receiving multiple parameters
const choosePokemonArrow = (firstPokemon, secondPokemon) =>
`I choose ${firstPokemon} and ${secondPokemon}!`;
console.log(choosePokemonArrow("Squirtle", "Bulbasaur"));
@NyaGarcia
NyaGarcia / arrow-function-no-params.js
Created March 5, 2021 14:55
Arrow function receiving no parameters
const sayHello = () => "Hello!";
console.log(sayHello());
@NyaGarcia
NyaGarcia / traditional-function-multiple.js
Created March 5, 2021 14:08
Traditional function receiving multiple parameters
function choosePokemon(firstPokemon, secondPokemon) {
return `I choose ${firstPokemon} and ${secondPokemon}!`;
}
console.log(choosePokemon("Charmander", "Squirtle"));
// Output: I choose Charmander and Squirtle!
@NyaGarcia
NyaGarcia / arrow-function-constructor-error.js
Created March 5, 2021 13:52
Error thrown when attempting to use an arrow function as a constructor
const User = () => {};
const user = new User();
// Output: Error: User is not a constructor
const choosePokemonArrow2 = pokemon => {
console.log("Logging");
return `I choose you ${pokemon}!`;
};
console.log(choosePokemonArrow2("Bulbasaur"));
/* Output:
Logging
I choose you Bulbasaur!
*/