Last active
August 29, 2015 14:00
-
-
Save rvbsanjose/11204118 to your computer and use it in GitHub Desktop.
This file contains 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
=================================================== | |
// In case you didn't know, there is a Math object | |
// with all sorts of cool methods attached to it | |
// Let's take a look at Math.max(); | |
// Call Math.max with an array of numbers | |
// i.e. Math.max(1,5,75,4,11); | |
Math.max(1, 2, 3, 4); | |
// Now declare a variable allNums that references a | |
// array of numbers | |
var allNums = [117, 341, 56, 17]; | |
// Now use apply to use Math.max() on allNums | |
Math.max.apply(this, allNums); | |
==================================================== | |
// Write two functions, sum and square, now write | |
// another function called squareOfSum that accepts two | |
// functions(sum and square) and returns a new function | |
// that computes square(sum()); | |
// Now, here's this trick, do it using call() and apply() | |
function sum(num1, num2) { return num1 + num2; } | |
function square(num) { return num * num; } | |
function squareOfSum(fn1, fn2) { | |
return function () { | |
return fn1.call(this, fn2.apply(this, arguments)); | |
} | |
} | |
var myFunc = squareOfSum(square, sum); | |
myFunc(2,3); | |
// Using apply to execute functions with an array | |
// of arguments. | |
// This is used for creating functions that | |
// can accept any number of arguments as opposed | |
// to a fixed number. | |
====================================================== | |
// Create an object called reindeer with properties dancer, | |
// prancer, and comet and assign them qualities | |
// (ex. Dancer: most dexterous, prancer: most cheerful.. etc) | |
// and then create the property Rudolf assigning it “the most | |
// famous reindeer of all”. | |
var reindeer = { | |
dancer: 'most dexterous', | |
prancer: 'most cheerful', | |
comet: 'most sleepy', | |
rudolf: 'the most famous reindeer of all' | |
}; | |
// now, create a method called recall which console logs | |
// the most famous reindeer of all. Call this function using call. | |
function recall() { console.log(this.rudolf); } | |
recall.call(reindeer); | |
====================================================== | |
// Create a function called snowball with a closure inside | |
// called dirt with a closure inside it called bigRock and | |
// call a function which dynamically logs the sentence “Ouch! | |
// the snowball has a bigRock inside it!” using the keyword “this" | |
// instead of hardcoding bigRock. call this function using .call | |
function snowball() { | |
return function dirt() { | |
return function bigRock() { | |
return function () { console.log(this.message); } | |
} | |
} | |
} | |
var message = "Ouch! The snowball has a bigRock inside it!" | |
snowball()()().call(this); | |
====================================================== | |
// Create a function called snow which passes the value | |
// of “this" to a closure called flake and then console | |
// logs that the value of this from the above function. | |
function snow(context) { | |
return function flake() { | |
console.log(context); | |
} | |
} | |
snow(this)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment