this指向注意點

2021-10-01 12:18:32 字數 527 閱讀 6495

下面的兩段**完美的說明了this指向,誰呼叫指向誰

var length=10;

functionfn(

)var obj=};

obj.q(

)//4

var length=10;

functionfn(

)var obj=

}obj.

method

(fn,

123)

//2

var length=10;

functionfn(

)var obj=

}obj.

method()

//10

//這裡之所以返回10,是因為雖然是obj.method呼叫,但是呼叫後,只是執行了method方法,

//讓fn開始執行,fn執行時根據作用域鏈找到最上面的fn方法,而這時的this就指向window

ES6 使用類的注意點和this指向問題

一 注意點 注意點1 es6中,類沒有變數提公升,必須先定義類,再例項化物件 錯誤寫法 報錯uncaught referenceerror cannot access star before initialization var starobj newstar class star jump 正確寫...

JS知識點 this指向

this的指向在函式定義的時候是確定不了的,只有函式執行的時候才能確定,this最終指向呼叫它的物件。1.函式呼叫模式 當乙個函式並非乙個物件的屬性時,那麼它就是被當做函式來呼叫的。在此種模式下,this被繫結為全域性物件,在瀏覽器環境下就是window物件 functiona a 2.方法呼叫模式...

C 注意防止指標指向空

例子 static size t sn class employee employee const std string s name new std string s size t getid const std string getname const void print const empl...