JavaScript對象的property屬性詳解_基礎知識

來源:互聯網
上載者:User

JavaScript中對象的property有三個屬性:
1.writable。該property是否可寫。
2.enumerable。當使用for/in語句時,該property是否會被枚舉。
3.configurable。該property的屬性是否可以修改,property是否可以刪除。

在ECMAScript 3標準中,上面三個屬性的值均為true且不可改:建立對象的property是可寫的、可被枚舉的、可刪除的;而在ECMAScript 5標準中,可通過property的描述對象(property descriptor)來對這些屬性進行配置和修改。

如果將property的值資訊也作為property的屬性來看的話,對象中的property擁有四個屬性:value、writable、enumerable和configurable。

對於用getter和setter方法來定義的property,由於其沒有writable屬性(property是否可寫取決於setter方法是否存在),因此這種property也有四個屬性:get、set、enumerable和configurable — get和set屬性的值為function。

擷取對象property的屬性

ECMAScript 5標準中,可以通過Object.getOwnPropertyDescriptor()來擷取對象自身某個property的屬性資訊:

複製代碼 代碼如下:

var o = {x:1};
var a = Object.create(o);
a.y = 3;
console.log(Object.getOwnPropertyDescriptor(a, "y"));//Object {configurable=true, enumerable=true, writable=true, value=3}
console.log(Object.getOwnPropertyDescriptor(a, "x"));//undefined

可以看到,如果property不存在或者property繼承自原型對象,則返回undefined。

設定對象property的屬性

ECMAScript 5標準中,可以通過Object.defineProperty()來設定對象自身某個property的屬性:

複製代碼 代碼如下:

Object.defineProperty(a, "y", {
    value:3,
    writable:true,
    enumerable:false,
    configuration:true
});
console.log(a.propertyIsEnumerable("y"));//false

如果設定的property是從原型對象中繼承而來的,那麼JavaScript將在對象自身中建立一個同名的property,這與賦值操作的相關行為一致:
複製代碼 代碼如下:

Object.defineProperty(a, "x", {
    value:1,
    writable:true,
    enumerable:false,
    configuration:true
});
console.log(a.propertyIsEnumerable("x"));//false
console.log(o.propertyIsEnumerable("x"));//true

除了修改property的屬性,還可以將property改為用getter或setter訪問:
複製代碼 代碼如下:

Object.defineProperty(a, "y", {
    get:function(){return 42;}
});
console.log(a.y);//42

在使用Object.defineProperty()時,property描述對象中的屬性值可以部分忽略,當屬性值有所忽略時,JavaScript中的處理規則如下:

如果property是建立的,則所有忽略的屬性值均為false或undefined。
如果property已存在,則所有忽略的屬性值維持原樣不變。


大量設定對象property的屬性

如果需要一次性設定多個property的屬性,可以使用Object.defineProperties()語句。該語句將返回修改後的對象。

複製代碼 代碼如下:

Object.defineProperties(a, {
    "y":{value:79, writable:true, enumerable:true, configurable:true},
    "z":{value:99, writable:true, enumerable:true, configurable:true}
});
console.log(a);//Object {y=79, z=99}

property屬性設定規則

當對property屬性進行修改時,必須遵循以下規則。如果違反規則,JavaScript將報TypeError錯誤:

如果對象不是extensible的,則只能修改已有property的屬性,無法添加新的property。
如果property的configurable屬性為false,則無法修改configurable和enumerable屬性的值,對於writable屬性,可以將其從true改為false,但無法將其從false改為true。如果property由getter和setter定義,則getter和setter方法無法被修改。
如果property的configurable屬性和writable屬性均為false,則property值不可改。如果property的writable屬性為false,但其configurable屬性為true,則property值依然可以修改。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.