Skip to content

Instantly share code, notes, and snippets.

@timhobbs

timhobbs/sandbox.js

Created Jun 3, 2015
Embed
What would you like to do?
Javascript sandbox
/**
* 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
You can’t perform that action at this time.