0
A
回答
2
this
由其出現的範圍定義。所以,你不能在對象字面上使用你想要的方式,因爲對象字面沒有自己的作用域。
this
具有由當前作用域(即,全局作用域或函數或方法的作用域)給出的含義。你可以這樣做:
var obj = {
name : 'object',
itis : 'object'
}
或本:
function Obj() {
this.name = 'object';
this.itis = this.name;
}
var obj = new Obj();
但你試圖將無法正常工作的。下面是行動問題的例子:
function Foo() {
this.name = "My function";
var obj = {
name : 'My literal object',
itis : this.name
}
alert(obj.itis); // "My function"
}
Foo();
警報顯示My function
因爲this
中的Foo
範圍定義; obj
沒有自己的範圍。
0
this
即使您在一個內部使用該關鍵字,關鍵字也不會引用該對象。它只是指啓動當前範圍的東西。
0
this
指對象obj
當你在函數傳遞this
:
var obj = {
name : 'object',
itis : function(){
return this.name;
}
}
obj.itis();//'object'
2
代碼
var obj = { name: 'object';
itis: this.name };
是完全等效於
var obj = {};
obj.name = 'object';
obj.itis = this.name;
換句話說this
參考當前外部範圍的this
,與新創建的對象無關。
事情是不同與
var obj = {};
obj.name = 'object';
obj.itisf = function(){ return this.name; }
console.log(obj.itisf()); // Shows "object"
,它是因爲this
,執行功能後,將成爲對象。
一個簡單的合理化是,當您使用.
從對象中獲取函數後,該函數中的this
將成爲對象。該規則還暗示在Javascript:
obj.f();
和
var ff = obj.f;
ff();
到不只在代碼中f
執行做同樣的事情,因爲在第一種情況下的this
值將成爲對象。在第二種情況下,而不是this
將成爲全局對象。
0
'this'關鍵字是指當前的作用域,在你的情況下,你在對象字面聲明中使用'this',我想,'this'是指'Window'對象。
如果我們看到語法明智,就不會有錯誤。但在運行時它將是'未定義'。看看下面的例子
var xyz = {
a: 10,
b: 20,
c: this.a + this.b
};
document.write(xyz.a," and ",xyz.b);
document.write(xyz.c);
// to cross check
document.write(Window.a + Window.b);
// Why NaN ?
document.write(undefined + undefined);
相關問題
- 1. 使用「this」爲對象
- 2. Inhertitance +使用對象(this)作爲參數
- 3. 使用`this`在JS對象
- 4. javascript在全局對象中使用'this'
- 5. 在JavaScript中使用'this'創建對象
- 6. 使用「this」關鍵字的對象
- 7. 在JavaScript中獲取'this'對象而不使用'this'這個詞
- 8. 使用錯誤'this'值的CountDown計時器對象
- 9. 使用new $ this創建對象
- 10. 使用'this'與多個對象
- 11. SyncHashtable this [對象鍵]不使用鎖定
- 12. 使用`this`與對象文字
- 13. 在對象中使用或不使用'this'
- 14. (使用$ this當不在對象上下文中)使用延伸
- 15. 如何使用'this'作爲另一個對象的參數?
- 16. Javascript Number對象的方法中的「this」如何轉換爲對象的數值?
- 17. Javascript中使用對象值
- 18. 在用於構造對象的靜態方法中使用「this」
- 19. 使用變量作爲對象的值
- 20. 使用值對象
- 21. 在Codeigniter的對象上下文中使用$ this的時候
- 22. 如何在javascript中使用對象值作爲對象名稱?
- 23. jQuery中的'this'對象
- 24. Javascript this this to Window對象
- 25. 如何訪問Angular 2中的對象屬性使用(this)
- 26. 爲什麼`this`引用全局對象?
- 27. 如何使用performSelector爲對象賦值
- 28. 使用JSON對象作爲整數值
- 29. 過多使用$ this作爲PHP中的對象參數,如何避免?
- 30. 使用HTML5音頻和jQuery調用對象(this)的引用
'this'指的是什麼? – 2014-12-13 07:16:33
'this'不是指'obj',它指的是這個範圍內的任何this。 – deceze 2014-12-13 07:17:53