JavaScript中的this基本問題實例小結(jié)
本文實例講述了JavaScript中的this基本問題.分享給大家供大家參考,具體如下:
在函數(shù)中 this 到底取何值,是在函數(shù)真正被調(diào)用執(zhí)行的時候確定下來的,函數(shù)定義的時候確定不了。
執(zhí)行上下文環(huán)境 :
**定義**:執(zhí)行函數(shù)的時候,會產(chǎn)生一個上下文的對象,里面保存變量,函數(shù)聲明和this。
**作用**:用來保存本次運行時所需要的數(shù)據(jù)
當你在代碼中使用了 this,這個 this 的值就直接從執(zhí)行的上下文中獲取了,而不會從作用域鏈中搜尋。
關(guān)于 this 的取值,大體上可以分為以下幾種情況:
情況一:全局 & 調(diào)用普通函數(shù)
在全局環(huán)境中,this 永遠指向 window。
console.log(this === window); //true
普通函數(shù)在調(diào)用時候(注意不是構(gòu)造函數(shù),前面不加 new),其中的 this 也是指向 window。
但是如果在嚴格模式下調(diào)用的話會報錯:
var x = 1;function first(){ console.log(this); // undefined console.log(this.x); // Uncaught TypeError: Cannot read property ’x’ of undefined}first();
情況二:構(gòu)造函數(shù)
所謂的構(gòu)造函數(shù)就是由一個函數(shù) new 出來的對象,一般構(gòu)造函數(shù)的函數(shù)名首字母大寫,例如像 Object,F(xiàn)unction,Array 這些都屬于構(gòu)造函數(shù)。
function First(){ this.x = 1; console.log(this); //First {x:1}}var first = new First();console.log(first.x); //1
上述代碼,如果函數(shù)作為構(gòu)造函數(shù)使用,那么其中的 this 就代表它即將 new 出來的對象。
但是如果直接調(diào)用 First函數(shù),而不是 new First(),那就變成情況1,這時候 First() 就變成普通函數(shù)。
function First(){ this.x =1; console.log(this); //Window}var first = First();console.log(first.x); //undefined
情況三:對象方法
如果函數(shù)作為對象的方法時,方法中的 this 指向該對象。
var obj = { x: 1, first: function () { console.log(this); //Object console.log(this.x); //1 }};obj.first();
注意:若是在對象方法中定義函數(shù),那么情況就不同了。
var obj = { x: 1, first: function () { function second(){ console.log(this); //Window console.log(this.x); //undefined } second(); }}obj.first();
可以這么理解:函數(shù) second雖然是在 obj.first 內(nèi)部定義的,但它仍然屬于一個普通函數(shù),this 仍指向 window。
在這里,如果想要調(diào)用上層作用域中的變量 obj.x,可以使用 self 緩存外部 this 變量。
var obj = { x:1, first: function () { var self = this; function second(){ console.log(self); //{x: 1} console.log(self.x); //1 } second(); }}obj.first();
如果 first 函數(shù)不作為對象方法被調(diào)用:
var obj = { x: 1, first: function () { console.log(this); //Window console.log(this.x); //undefined }};var fn = obj.first;fn();
obj.first 被賦值給一個全局變量,并沒有作為 obj 的一個屬性被調(diào)用,那么此時 this 的值是 window。
情況四:構(gòu)造函數(shù) prototype 屬性
function First(){ this.x = 1;}First.prototype.getX = function () { console.log(this); //First {x: 1, getX: function} console.log(this.x); //1}var first= new First();first.getX();
在 First.prototype.getX 函數(shù)中,this 指向的first 對象。不僅僅如此,即便是在整個原型鏈中,this 代表的也是當前對象的值。
情況五:函數(shù)用 call
var obj = { x:1}function first(){ console.log(this); //{x: 1} console.log(this.x); //1}first.call(obj);
當一個函數(shù)被 call調(diào)用時,this 的值就取傳入的對象的值。
來源:知乎
鏈接:https://zhuanlan.zhihu.com/p/25294187?utm_source=com.youdao.note&utm_medium=social
感興趣的朋友可以使用在線HTML/CSS/JavaScript前端代碼調(diào)試運行工具:http://tools.jb51.net/code/WebCodeRun測試上述代碼運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章:
1. HTML5 Canvas繪制圖形從入門到精通2. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明4. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁5. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式6. XML入門的常見問題(一)7. 使用EF Code First搭建簡易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移8. JavaWeb Servlet中url-pattern的使用9. asp批量添加修改刪除操作示例代碼10. ASP中if語句、select 、while循環(huán)的使用方法
