Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript illustrates that methods are the same as functions with an implicit parameter "this"
var completeSale = function(num) {
console.log("Sale " + num + ": selling " + this.items + " to " + this.customer);
}
var foo = {
customer: "Fred",
items: ["carrot","eggs"],
complete: completeSale
};
console.log("method style:")
foo.complete(99);
console.log("function style:")
completeSale.call(foo, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment