Created
June 3, 2015 18:44
-
-
Save timhobbs/a931d497b09ddbc98076 to your computer and use it in GitHub Desktop.
Javascript sandbox
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
/** | |
* Just playing around with Function.prototype.call | |
* jsfiddle: https://jsfiddle.net/jesus_tesh/e7ufpLmz/ | |
**/ | |
console.clear(); | |
console.log("Basic function call"); | |
console.log("===================\n"); | |
function foo(bar) { | |
console.log("bar", bar); | |
} | |
foo.call(this, "baz"); | |
console.log("\n"); | |
var chipmunks = [{ | |
name: "Alvin", | |
age: 8, | |
favoriteColor: "Red" | |
}, { | |
name: "Simon", | |
age: 9, | |
favoriteColor: "Blue" | |
}, { | |
name: "Theodore", | |
age: 7, | |
favoriteColor: "Green" | |
}]; | |
console.log("Anonymous function call"); | |
console.log("=======================\n"); | |
function loopit(func) { | |
for (c in chipmunks) { | |
(function () { | |
func(this); | |
}).call(chipmunks[c]); | |
} | |
} | |
loopit(function (c) { | |
console.log("name:", c.name, | |
"\t\tage:", c.age, | |
"\t\tfavorite color:", c.favoriteColor); | |
}); | |
console.log("\n"); | |
var sayHello = function (name) { | |
console.log("Hello, " + name + "!"); | |
}; | |
loopit(function (c) { | |
sayHello.call(this, c.name); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment