(编辑:jimmy 日期: 2024/11/20 浏览:2)
本文实例讲述了JS Object.preventExtensions(),Object.seal()与Object.freeze()用法。分享给大家供大家参考,具体如下:
Object.preventExtensions 只能阻止一个对象不能再添加新的自身属性,仍然可以为该对象的原型添加属性。
var jb51 = { name: "zuojj" }; Object.preventExtensions(jb51); jb51.url = "https://www.jb51.net"; //Outputs: undefined console.log(jb51.url);
但是在严格模式下会报错:
"use strict"; var jb51 = { name: "zuojj" }; Object.preventExtensions(jb51); jb51.url = "https://www.jb51.net"; //Outputs: Uncaught TypeError: Can't add property url, object is not extensible console.log(jb51.url);
var jb51 = { name: "zuojj" }; Object.preventExtensions(jb51); jb51.url = "https://www.jb51.net"; //Outputs: Uncaught TypeError: Can't add property url, object is not extensible console.log(jb51.url);
var jb51 = { name: "jb51", url : "https://www.jb51.net" }; 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: "https://www.jb51.net" }); //Outputs: Uncaught TypeError: Cannot define property:url, object is not extensible. console.log(jb51);
var jb51 = { name: "jb51", url : "https://www.jb51.net" }; Object.preventExtensions(jb51); //Outputs: false console.log(Object.isExtensible(jb51));
var jb51 = { name: "jb51" }, sealed = Object.seal(jb51), frozen = Object.freeze(jb51); //Outputs: false false console.log(Object.isExtensible(sealed), Object.isExtensible(frozen));
var jb51 = { name: "zuojj", url: "https://www.jb51.net" }; Object.seal(jb51), jb51.sex = "male"; jb51.name = "jb51"; //Outputs: {name: "jb51", url: "https://www.jb51.net"} console.log(jb51);
var jb51 = { name: "jb51", url: "https://www.jb51.net" }; Object.seal(jb51), delete jb51.name; //Outputs: {name: "jb51", url: "https://www.jb51.net"} console.log(jb51);
var jb51 = { name: "jb51", url: "https://www.jb51.net" }; 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: false console.log(Object.isSealed(empty)); // 空对象 && 不可扩展 === 密封对象. Object.preventExtensions(empty); //Outputs: true console.log(Object.isSealed(empty)); // 非空对象 && 不可扩展对象不会变成密封对象,因为密封对象的所有自身属性必须是不可配置的. var hasProp = { fee: "fie foe fum" }; Object.preventExtensions(hasProp); //Outputs: false console.log(Object.isSealed(hasProp)); // 如果把这个属性变的不可配置,则这个对象也就成了密封对象. Object.defineProperty(hasProp, "fee", { configurable: false }); //Outputs: true console.log(Object.isSealed(hasProp)); console.log("-------------"); // 一个密封对象也可以是一个冻结对象,但不是绝对的. var s1 = {}; Object.seal(s1); //Outputs: true console.log(Object.isFrozen(s1)); var s2 = Object.seal({ p: 3 }); //Outputs: false console.log(Object.isFrozen(s2)); // 属性"p"可写
var obj = { name: "jb51", url: "https://www.jb51.net" }; Object.freeze(obj); obj.sex = "male"; obj.name = "zuojj"; //Outputs: {name: "jb51", url: "https://www.jb51.net"} console.log(obj);
var obj = { name: "jb51", url: "https://www.jb51.net" }; Object.freeze(obj); delete obj.name; //Outputs: {name: "jb51", url: "https://www.jb51.net"} console.log(obj);
var obj = { name: "jb51", url: "https://www.jb51.net" }; Object.freeze(obj); //Outputs: true console.log(Object.isFrozen(obj));
Object.preventExtensions(),Object.isExtensible(),
Object.seal(),Object.isSealed(),
Object.freeze(),Object.isFrozen()方法是ES5规范的一部分,IE8及以下低版本浏览器不支持。Oprea不支持。
感兴趣的朋友还可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试运行上述js代码。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。