在window.setTimeout和全局环境中的匿名方法中的this是指向window对象的,所以在调用过程中可以将此匿名方法做为实际的某个对象(如this对象)的方法来调用.
var foo = 'this is window.foo!';var d = [1, 2, 3];// timeout functionsfunction Constructor() { this.foo = 'this is Constructor.foo!'; var that = this; this.timerId = window.setTimeout(function() { // alert(this); // will get [object Window] alert("this.foo = " + this.foo); alert("that.foo = " + that.foo); }, 1000);}// local functionsConstructor.prototype.getFoo = function() { alert(this); // [object Object] var getExternalFoo = (function() { // alert(this); // will get [object Window] return d.concat(this.foo) })(); return getExternalFoo;};// using Function.call(object)Constructor.prototype.getBar = function() { var getInternalFoo = (function() { // alert(this); // will get [object Object] return d.concat(this.foo) }).call(this); return getInternalFoo;};var f = new Constructor();console.log(f.getFoo());console.log("<br />");console.log(f.getBar()); |