Javascript Booster
@GoyeSays
@GoyeSays
https://github.com/Goye
var characters = [
{ name: 'Alfred', occupation: 'Butler', age: 70},
{ name: 'Batman', occupation: 'CEO of Wayne Enterprises', age 40},
{ name: 'Harley Quinn', occupation: 'Former psychiatrist', age: 29},
{ name: 'Joker', occupation: 'Vilain', age: 35}
];
var occupations = characters.map((character) => {
return character.occupation;
});
console.log(occupations);
var characters = [
{ name: 'Alfred', occupation: 'Butler', age: 70},
{ name: 'Batman', occupation: 'CEO of Wayne Enterprises', age 40},
{ name: 'Harley Quinn', occupation: 'Former psychiatrist', age: 29},
{ name: 'Joker', occupation: 'Vilain', age: 35}
];
var totalAge = characters.reduce((sum, character) => {
return sum + character.age;
}, 0);
console.log(totalAge);
var charactersObject = characters.reduce((array, character) => {
array.push(character.name);
return array;
}, [])
console.log(charactersObject);
A function is considered pure if it adheres to the following properties:
Pure add function
function add(x, y) {
return x + y;
}
Impure add function
let y = 12;
function add(x) {
return x + y;
}
This function is not pure because it references data that it hasn’t directly been given. As a result, it’s possible to call this function with the same input and get different output:
let y = 2;
add(3) // 5
y = 3;
add(3)
RHS reference (who's the source of the assignment)
console.log( a );
LHS reference (who's the target of the assignment)
a = 2;
RHS & LHS references
function foo(a) {
console.log( a ); // 2
}
foo( 2 );
var a = 2;
function myObject() { this.a = 2; };
var o = new myObject();
What's the difference?
var me = {firstName: 'Carlos', lastName: 'Goyeneche'};
var you = {firstName: 'John', lastName: 'Titor'};
function say(greeting) {
console.log(greeting + ' ' +
this.firstName + ' ' +
this.lastName);
}
say.call(me, 'Hello');
say.apply(me, ['Hello']);
var sayHelloJohn = say.bind(you);
sayHelloJohn();
console.log("Hello World");
setTimeOut(function(){
console.log("I am in the timeout function")
}, 0)
console.log("I am outside of the timeout");
Aren't promises just callbacks?
Aren't promises just callbacks?
var myPromise = new Promise(
// Asynchronous simulation
window.setTimeout(
function() {
resolve({
response: 'yei!'
});
}, 2000);
}
);
myPromise.then(
function(res) {
console.log(res);
})
.catch(
function(error) {
console.log('Houston we have problems.');
});
}
https://github.com/Goye
https://github.com/Goye