2014-02-12 27 views
0

我只是想知道,用「一個」,「兩個」,「三個」的東西,能有空間嗎?所以,而不是「一個」它可能是「一個喵」?對象文字中帶空格的屬性名稱

var meow = { one:  function (t) { return "a"; }, 
       two:  function (t) { return "b"; }, 
       three:  function (t) { return "c"; } 
       }; 
+0

參見[這](HTTPS ://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) –

回答

1

是的,但你不能再訪問屬性爲meow.one mewo,而不是你需要使用括號語法:meow['one mewo']

同樣,當你定義的對象,你需要使用引號鍵:

在屬性名
var meow = { 
    'one meow' : function (t) { return "a"; }, 
    two   : function (t) { return "b"; }, 
    'three meow': function (t) { return "c"; } 
};  
5

當然,可以有空間,但你必須將它們封閉在"

var meow = { 
      "one meow": function (t) { return "a"; }, 
      two:  function (t) { return "b"; }, 
      three:  function (t) { return "c"; } 
      }; 

如果以後訪問屬性,請使用括號語法:

console.log(meow["one meow"]());