2017-04-21 222 views
3

在控制檯中放置/更改"property"this[property]有什麼區別,我相信它們都是指同一個表達式,但是當我調用函數時,後者會給我[object,Object]。Javascript對象枚舉

var rockSpearguns = { 
    Sharpshooter: {barbs: 2, weight: 10, heft: "overhand"}, 
    Pokepistol: {barbs: 4, weight: 8, heft: "shoulder"}, 
    Javelinjet: {barbs: 4, weight: 12, heft: "waist"}, 
    Firefork: {barbs: 6, weight: 8, heft: "overhand"}, 
    "The Impaler": {barbs: 1, weight: 30, heft: "chest"} 
}; 

rockSpearguns["listGuns"] = function(){ 
    for (var property in this) { 
     if(this[property]["heft"]!==undefined){ 
      console.log("Behold! " + this[property] + ", with " + 
      this[property]["heft"] + " heft!"); 
     } 
    } 
} 

rockSpearguns["listGuns"](); 
+0

你問這個[「property」]'和'this [property]'有什麼區別? –

+0

nope,屬性和這[屬性]在控制檯 –

回答

2

property將循環對象beign(​​3210,"Pokepistol",...),這已經是串的鑰匙。

this[property]將是所有對象的對象的值。將對象與字符串進行協調時,通過調用其函數toString將該對象投射到字符串中。所以:

var obj = {}; 
var string = "Hello. I'm " + obj; 

是一樣的:在這種情況下

var obj = {}; 
var string = "Hello. I'm " + obj.toString(); 

obj.toString()將返回"[object Object]"

關於括號和點符號,這裏是關於MDN的文檔。

2

當在控制檯或一個簡單的腳本中訪問this,它是指window,所以是相同的訪問的變量,它是window範圍內。

var a = 123; // variable `a` is bound to `window` 
console.log(a); // 123 
console.log(window.a); // 123 
console.log(this.a); // 123 

但是,當你是一個函數或對象的內部,this指的是它自己的上下文:

function test() { 
    this.a = 123; 
    console.log(a); // 123 
    console.log(this.a); // 123 
    console.log(this); // test {a: 123} 
} 

new test(); // to create new context for this function, we need to call `new`, otherwise it will also be `window` 

console.log(typeof a); // undefined 
console.log(this.a); // undefined 
console.log(this); // window 

更多關於這一點:http://ryanmorr.com/understanding-scope-and-context-in-javascript/

+0

但他們是一樣的,除了在控制檯,不是嗎? –

+0

它可以通過'bind'方法改變,但通常當你不在一個函數或對象內時,'this'將引用'window'。 –

+0

謝謝,我會稍後再試。 –

2

裏面for環路thisrockSpearGuns對象。在這種情況下,this[property]將參考例如Firefork對象。您需要使用property而不是this[property]

試試這個:

var rockSpearguns = { 
 
    Sharpshooter: {barbs: 2, weight: 10, heft: "overhand"}, 
 
    Pokepistol: {barbs: 4, weight: 8, heft: "shoulder"}, 
 
    Javelinjet: {barbs: 4, weight: 12, heft: "waist"}, 
 
    Firefork: {barbs: 6, weight: 8, heft: "overhand"}, 
 
    "The Impaler": {barbs: 1, weight: 30, heft: "chest"} 
 
}; 
 
    
 
rockSpearguns["listGuns"]=function(){ 
 
    for (var property in this) { 
 
    if(this[property]["heft"]!==undefined){ 
 
     console.log("Behold! " + property + ", with " + 
 
     this[property]["heft"] + " heft!"); 
 
    } 
 
    } 
 
} 
 
    
 
rockSpearguns["listGuns"]();

+0

如果我沒有弄錯,它不是for循環,而是for循環:)。此外,爲什麼當您嘗試在JS Bin這樣的站點中調試它時,總是需要在訪問對象和對象屬性時使用括號和點符號的組合? –

+0

選中此項:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors 如果您使用變量來標識對象的屬性,那麼您需要使用括號。 – kotapeter

2

讓我們來剖析一點一滴:

rockSpearguns["listGuns"]()電話是另一種說法: rockSpearguns.listGuns(); 這意味着「this」的上下文被設置爲rockSpearguns對象(在呼叫站點)。

當JS試圖評估這個[property]值時,它看到這個對象的值被綁定到rockSpearguns,而「property」是對象屬性的字符串的枚舉值(由於for-in循環)。但是,這[屬性]僅僅意味着this.property(點符號)。 在這種情況下,this.property是一個對象(Sharpshooter等) 現在,當我們試圖連接字符串時,需要通過調用返回[object,Object]的toString()方法將Sharpshooter對象轉換爲字符串。