本文實例講述了JS Object.preventExtensions(),Object.seal()與Object.freeze()用法。分享給大家供大家參考,具體如下:
Method | Description | Param | Detail |
---|---|---|---|
Object.preventExtensions() | 讓一個對象變的不可擴展,也就是永遠不能再添加新的屬性 | Object | 詳細 |
Object.isExtensible() | 判斷一個對象是否是可擴展 | Object | 詳細 |
Object.seal() | 讓一個對象密封,并返回被密封后的對象。密封對象是指那些不能添加新的屬性,不能刪除已有屬性,以及不能修改已有屬性的可枚舉性、可配置性、可寫性,但可能可以修改已有屬性的值的對象。 | Object | 詳細 |
Object.isSealed() | 判斷一個對象是否是密封的 | Object | 詳細 |
Object.freeze() | 凍結一個對象并返回被凍結的對象。凍結對象是指那些不能添加新的屬性,不能修改已有屬性的值,不能刪除已有屬性,以及不能修改已有屬性的可枚舉性、可配置性、可寫性的對象。也就是說,這個對象永遠是不可變的。 | Object | 詳細 |
Object.isFrozen() | 判斷一個對象是否被凍結 | Object | 詳細 |
Object.preventExtensions 只能阻止一個對象不能再添加新的自身屬性,仍然可以為該對象的原型添加屬性。
var jb51 = { name: "zuojj"};Object.preventExtensions(jb51);jb51.url = "http://www.49028c.com";//Outputs: undefinedconsole.log(jb51.url);
但是在嚴格模式下會報錯:
"use strict";var jb51 = { name: "zuojj"};Object.preventExtensions(jb51);jb51.url = "http://www.49028c.com";//Outputs: Uncaught TypeError: Can't add property url, object is not extensibleconsole.log(jb51.url);
var jb51 = { name: "zuojj"};Object.preventExtensions(jb51);jb51.url = "http://www.49028c.com";//Outputs: Uncaught TypeError: Can't add property url, object is not extensibleconsole.log(jb51.url);
var jb51 = { name: "jb51", url : "http://www.49028c.com"};Object.preventExtensions(jb51);delete jb51.url;//Outputs: {name: "jb51"}console.log(jb51);
var jb51 = { name: "jb51"};Object.preventExtensions(jb51);jb51 = Object.defineProperty(jb51, "url", {value: "http://www.49028c.com" });//Outputs: Uncaught TypeError: Cannot define property:url, object is not extensible.console.log(jb51);
var jb51 = { name: "jb51", url : "http://www.49028c.com"};Object.preventExtensions(jb51);//Outputs: falseconsole.log(Object.isExtensible(jb51));
var jb51 = { name: "jb51"},sealed = Object.seal(jb51),frozen = Object.freeze(jb51);//Outputs: false falseconsole.log(Object.isExtensible(sealed), Object.isExtensible(frozen));
var jb51 = { name: "zuojj", url: "http://www.49028c.com"};Object.seal(jb51),jb51.sex = "male";jb51.name = "jb51";//Outputs: {name: "jb51", url: "http://www.49028c.com"}console.log(jb51);
var jb51 = { name: "jb51", url: "http://www.49028c.com"};Object.seal(jb51),delete jb51.name;//Outputs: {name: "jb51", url: "http://www.49028c.com"}console.log(jb51);
var jb51 = { name: "jb51", url: "http://www.49028c.com"};Object.seal(jb51);Object.defineProperty(jb51, "sex", {value: "male"})//Outputs: Uncaught TypeError: Cannot define property:sex, object is not extensible.console.log(jb51);
上面說到密封對象不可擴展,所以會報錯
// 新建的對象默認不是密封的.var empty = {};//Outputs: falseconsole.log(Object.isSealed(empty));// 空對象 && 不可擴展 === 密封對象.Object.preventExtensions(empty);//Outputs: trueconsole.log(Object.isSealed(empty));// 非空對象 && 不可擴展對象不會變成密封對象,因為密封對象的所有自身屬性必須是不可配置的.var hasProp = { fee: "fie foe fum" };Object.preventExtensions(hasProp);//Outputs: falseconsole.log(Object.isSealed(hasProp));// 如果把這個屬性變的不可配置,則這個對象也就成了密封對象.Object.defineProperty(hasProp, "fee", { configurable: false });//Outputs: trueconsole.log(Object.isSealed(hasProp));console.log("-------------");// 一個密封對象也可以是一個凍結對象,但不是絕對的.var s1 = {};Object.seal(s1);//Outputs: trueconsole.log(Object.isFrozen(s1));var s2 = Object.seal({ p: 3 });//Outputs: falseconsole.log(Object.isFrozen(s2)); // 屬性"p"可寫
var obj = { name: "jb51", url: "http://www.49028c.com"};Object.freeze(obj);obj.sex = "male";obj.name = "zuojj";//Outputs: {name: "jb51", url: "http://www.49028c.com"}console.log(obj);
var obj = { name: "jb51", url: "http://www.49028c.com"};Object.freeze(obj);delete obj.name;//Outputs: {name: "jb51", url: "http://www.49028c.com"}console.log(obj);
var obj = { name: "jb51", url: "http://www.49028c.com"};Object.freeze(obj);//Outputs: trueconsole.log(Object.isFrozen(obj));
Object.preventExtensions(),Object.isExtensible(),
Object.seal(),Object.isSealed(),
Object.freeze(),Object.isFrozen()方法是ES5規范的一部分,IE8及以下低版本瀏覽器不支持。Oprea不支持。
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答