這個phonebookEntry = {};
是一個空的對象,沒有任何屬性。
其實,這相當於該phonebookEntry = new Object();
您可以驗證這是簡單的,如下:
typeof(phonebookEntry)
將輸出"object"
。
在JavaScript
我們必須創造一個像上面,後來空物體附着有意義給我們,像下面的任何財產的能力:
phonebookEntry.name = 'Oxnard Montalvo';
或等價
phonebookEntry["name"] = 'Oxnard Montalvo';
此外,後者被稱爲括號記號。
因爲它在MDN,在那裏你會找到關於此主題的全面和詳細的方法指出:
JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. This chapter describes how to use objects, properties, functions, and methods, and how to create your own objects.
在上面的代碼中,我們定義的屬性name
,與主要name
和值'奧克斯納德蒙塔爾沃'。
它指定'phonebookEntry'是一個對象,以便爲它添加屬性。 [MDN |使用對象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) –
在JavaScript表達式中,「{}」只是創建一個新的空對象。 – Pointy