Prototype JavaScript framework: Function.bindPrototype JavaScript framework: Function.bind
Prototype JavaScript framework: Function.bind
window.name = "the window object"
function scopeTest() {
return this.name
}
// calling the function in global scope:
scopeTest()
// -> "the window object"
var foo = {
name: "the foo object!",
otherScopeTest: function() { return this.name }
}
foo.otherScopeTest()
// -> "the foo object!"
// ... continuing from the last example
// note that we aren't calling the function, we're simply referencing it
window.test = foo.otherScopeTest
// now we are actually calling it:
test()
// -> "the window object"
var obj = {
name: 'A nice demo',
fx: function() {
alert(this.name);
}
};
window.name = 'I am such a beautiful window!';
function runFx(f) {
f();
}
var fx2 = obj.fx.bind(obj);
runFx(obj.fx);
runFx(fx2);
沒有留言:
張貼留言