Skip to content

Instantly share code, notes, and snippets.

@danilosilvadev
Last active October 15, 2017 10:42
Show Gist options
  • Save danilosilvadev/27e25397829f59d805aec91e035bad3d to your computer and use it in GitHub Desktop.
Save danilosilvadev/27e25397829f59d805aec91e035bad3d to your computer and use it in GitHub Desktop.
JS Beyond is some snippets with core concepts of js that is important and is not basics.
var array = [1, 2, 3];
var newArra = array.filter(function(item){
return item > 2;
});
console.log(newArray); //[3]
//is the same result as:
var newArray =[];
function pushIntoNewArray(array){
for(var i = 0; i < array.length; i++){
if(array[i] > 2){
newArray.push(array[i]);
}
}
return newArray;
}
console.log(pushIntoNewArray(newArray)); //[3]
//Fiter creates a new array with just the elements that passed in the test of the array that you used it.
var array = [1, 2, 3];
var newArra = array.map(function(item){
return item*2;
});
console.log(newArray); //[2, 4, 6]
//is the same result as:
var newArray =[];
function pushIntoNewArray(array){
for(var i = 0; i < array.length; i++){
array[i] = array[i]*2;
newArray.push(array[i]);
}
return newArray;
}
console.log(pushIntoNewArray(newArray)); //[2, 4, 6]
//Map is a callback creates a new array from each element of the array that you used it.
//"this binding has nothing to do with where a function is declared,
//but has instead everything to do with the manner in which the function is called."
//Look for the call-site.
//`this` questions:
//"Is the function called with new (new binding)? If so, this is the newly constructed object.
var bar = new foo()
//Is the function called with call or apply (explicit binding),
//even hidden inside a bind hard binding? If so, this is the explicitly specified object.
var bar = foo.call( obj2 )
//Is the function called with a context (implicit binding),
//otherwise known as an owning or containing object? If so, this is that context object.
var bar = obj1.foo()
//Otherwise, default the this (default binding). If in strict mode,
//pick undefined, otherwise pick the global object.
var bar = foo()"
/*
"Determining the this binding for an executing function requires finding
the direct call-site of that function. Once examined, four rules
can be applied to the call-site, in this order of precedence:
Called with new? Use the newly constructed object.
Called with call or apply (or bind)? Use the specified object.
Called with a context object owning the call? Use that context object.
Default: undefined in strict mode, global object otherwise."
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment