Skip to content

Instantly share code, notes, and snippets.

@timhobbs
Created June 3, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timhobbs/a931d497b09ddbc98076 to your computer and use it in GitHub Desktop.
Save timhobbs/a931d497b09ddbc98076 to your computer and use it in GitHub Desktop.
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