原创转载请注明出处:http://agilestyle.iteye.com/blog/2341901
Preventing Object Modification
When you want to lock down an object’s properties in some way, there are three different ways to do so.
Preventing Extensions
If you use Object.preventExtensions(), objects will no longer allow properties to be added.
var person1 = { name: "Nicholas" }; console.log(Object.isExtensible(person1)); // true Object.preventExtensions(person1); console.log(Object.isExtensible(person1)); // false person1.sayName = function () { console.log(this.name); }; console.log("sayName" in person1); // false
Sealing Objects
You could also create a sealed object with the Object.seal() method, which makes that object non-extensible and makes its properties nonconfigurable.
var person1 = { name: "Nicholas" }; console.log(Object.isExtensible(person1)); // true console.log(Object.isSealed(person1)); // false Object.seal(person1); console.log(Object.isExtensible(person1)); // false console.log(Object.isSealed(person1)); // true person1.sayName = function() { console.log(this.name); }; console.log("sayName" in person1); // false person1.name = "Greg"; console.log(person1.name); // "Greg" delete person1.name; console.log("name" in person1); // true console.log(person1.name); // "Greg" var descriptor = Object.getOwnPropertyDescriptor(person1, "name"); console.log(descriptor.configurable); // false
Freezing Objects
The Object.freeze() method creates a frozen object, which is a sealed object with nonwritable data properties.
var person1 = { name: "Nicholas" }; console.log(Object.isExtensible(person1)); // true console.log(Object.isSealed(person1)); // false console.log(Object.isFrozen(person1)); // false Object.freeze(person1); console.log(Object.isExtensible(person1)); // false console.log(Object.isSealed(person1)); // true console.log(Object.isFrozen(person1)); // true person1.sayName = function() { console.log(this.name); }; console.log("sayName" in person1); // false person1.name = "Greg"; console.log(person1.name); // "Nicholas" delete person1.name; console.log("name" in person1); // true console.log(person1.name); // "Nicholas" var descriptor = Object.getOwnPropertyDescriptor(person1, "name"); console.log(descriptor.configurable); // false console.log(descriptor.writable); // false
Be careful with nonextensible objects, and always use strict mode so that attempts to access the objects incorrectly will throw an error.
Reference
Leanpub.Principles.of.Object-Oriented.Programming.in.JavaScript.Jun.2014
相关推荐
"javascript学习视频"这个资源,尤其是“妙味课堂原创JavaScript视频教程——事件详解3课资料”,旨在帮助学习者深入理解JavaScript中的事件处理机制,这对于创建交互式网页至关重要。 事件是JavaScript的核心特性...
oriented programming language support data abstraction by preventing an object from being manipulated except via its defined external operations.
Hinton在2012年提出的Improving neural networks by preventing co-adaptation of feature detectors也就是dropout,常用于防止训练过拟合,主要是通过随机选择一部分神经元训练,而直接丢弃其他神经元.
### JavaScript Ninja Secrets: Key Insights from "Secrets of the JavaScript Ninja, 2nd Edition" In the world of web development, JavaScript has become the lingua franca, powering applications across ...
根据提供的文件信息,本文将对如何防止Android应用被反编译进行深入探讨,并结合文档标题“Preventing Reverse.pdf”及描述“防止Android调试,不过全英文的,建议各取所需”,来提炼出与之相关的重要知识点。...
Once an object is deemed unreachable, the garbage collector reclaims the associated memory, preventing memory leaks. 7. What is a Java package, and why is it used?Answer: A Java package is a ...
《数学建模-2005 C O Preventing the Hydrocalypse获奖作品》是一个与数学建模相关的压缩包,其中包含了一份2005年的获奖论文。这份论文的主题是“防止水灾末日”(Preventing the Hydrocalypse),明显是针对水资源...
Networkers2009:BRKSEC-2002 - Understanding and Preventing Layer 2 Attacks
信息安全_数据安全_Preventing Your Physical Access 数据安全 威胁情报 安全架构 安全防护 法律法规
Fixed an issue which was preventing SQL Prompt from launching when upgrading from a previous version. SP-7560 : Fixed an issue which would sometimes cause unwanted newlines to be inserted into scripts...
5. **源码示例**:展示如何用不同编程语言(如JavaScript、C#或C++)编写代码来实现防止意外拖放的逻辑。 6. **使用现成的库和框架**:推荐一些开源库或框架,如jQuery UI、React DnD等,它们提供了一套完整的拖放...
报告“Preventing Financial Crime in Cryptoassets”关注的是如何在虚拟货币领域防止金融犯罪,特别是针对非法资金流动的调查。这份资料对于执法机构来说是至关重要的工具,因为它详细介绍了多种涉及虚拟货币的诈骗...
「安全知识」Preventing_an_Enterprise_Win10_Rollout_Being_Remotely_Controlled_and_Ransomed - 自动化 日志审计 漏洞挖掘 WEB应用防火墙 安全体系 安全资讯
1. 当句子缺少谓语动词时,括号内的动词应作为谓语动词,如例句(3):He **was preventing** that a tiger toy was real and giving it a voice. 2. 如果句中有并列连词(如and)和括号内的动词并列,该动词是谓语...
"信息安全_数据安全_Leaders Needed Preventing The Next Big Breach" 这个标题强调了领导力在防止重大安全事件中的核心作用。Equifax 的首席信息安全官Jamil Farshchi在RSA会议中提出,领导层的态度和决策对于塑造...
标题中的"Preventing an Enterprise Win10 R"着重强调了企业在进行Windows 10大规模升级时,如何避免系统被远程控制或遭受勒索软件攻击的问题。描述中提到的安全体系、数据安全和安全建设,是确保企业信息安全的基石...
在"Preventing bit stuffing in CAN"这篇论文中,作者探讨了如何在CAN分析中有效地预防比特位填充错误。这些错误可能由于硬件设计缺陷、软件实现错误或者电磁干扰等因素导致。论文可能涉及了错误检测和恢复策略,如...