3 回答

TA貢獻1963條經驗 獲得超6個贊
class
作為一種方法
var foo = {};foo.someMethod = function(){ alert(this);}
作為A函數
var foo = function(){ alert(this); } foo();
這可能就是你被絆倒的原因
var foo = {};foo.someMethod = function (){ var that=this; function bar(){ alert(that); }}
use strict
this
undefined
).
作為一個建設者
function Foo(){ this.confusing = 'hell yeah';}var myObject = new Foo();
應用方法
function foo(a,b){ alert(a); alert(b); alert(this);}var args = ['ah','be'];foo.apply('omg',args);

TA貢獻1886條經驗 獲得超2個贊
這是定義的行為嗎?跨瀏覽器安全嗎?
有什么理由可以解釋為什么它是這樣的.
this
如果 this
在構造函數中使用,并使用 new
關鍵詞, this
引用將要創建的對象。 this
即使在公開的方法中也會繼續意味著目標。 如果 this
在其他任何地方使用,包括嵌套的 受保護
函數,它引用全局范圍(在瀏覽器中是窗口對象)。
添加回答
舉報