This repository was archived by the owner on Jan 13, 2022. It is now read-only.
-
Couldn't load subscription status.
- Fork 1
bind
oatkiller edited this page Sep 13, 2010
·
3 revisions
returns a new function that does the same as the first, but is bound to the passed execution context
(function () {// an object with a 'name' property
var person1 = {
name: 'Robert'
};
// another object with a 'name' property
var person2 = {
name: 'Roy'
};
// a function that returns the name of the object it belongs to.
// only it doesnt belong to an object!
var get_name = function () {
return this.name;
};
// with bind, a function acts like its on an object, even tho its not
var get_person1s_name = get_name[o.bind](person1),
get_person2s_name = get_name[o.bind](person2);
get_person1s_name(); // returns 'Robert'
get_person2s_name(); // returns 'Roy'
})();