4 ms·
var foo; foo.name = "Hi! I'm foo." var bar; bar.name = "I'm bar." var alertMe = function(){ alert(this.name); } alertMe.call(foo); //calls 'ale
by sam_in_nyc 18y ago
var foo;
foo.name = "Hi! I'm foo."
var bar;
bar.name = "I'm bar."
var alertMe = function(){ alert(this.name); }
alertMe.call(foo); //calls 'alertMe' setting 'this' to be foo
alertMe.call(bar); //same, except setting 'this' to be bar
Pretty cool, right? Methods can inherit objects! You could always have done:
foo.alertMe = alertMe;
bar.alertMe = alertMe;
foo.alertMe();
bar.alertMe();