JavaScript Object Properties —— Preventing Object Modification

原创转载请注明出处: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 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error message typically occurs when the Java Virtual Machine (JVM) is unable to allocate the required amount of memory for the application. The JVM is responsible for managing the memory allocation for Java applications. There are several possible reasons why the JVM may not be able to allocate the required amount of memory. Here are some common causes and possible solutions: 1. Insufficient memory: The JVM may not have enough available memory on the system to allocate the requested heap size. You can try increasing the memory available to the JVM by setting the -Xmx flag to a higher value. For example, you can set -Xmx2g to allocate 2GB of memory. 2. 32-bit JVM limitation: If you are running a 32-bit JVM, it may not be able to allocate more than 2GB of memory. In this case, you can try running a 64-bit JVM instead. 3. Conflicting memory settings: If you have conflicting memory settings in your application or in the JVM startup parameters, it can cause the JVM to fail to allocate the required memory. You can try removing any conflicting settings or adjusting them to ensure they do not conflict. 4. Other system resource limitations: There may be other system resource limitations, such as disk space or CPU usage, that are preventing the JVM from allocating the required memory. You can try freeing up resources or adjusting the application to use fewer resources. Overall, the solution to this error message will depend on the specific cause. By identifying and addressing the underlying cause, you can resolve the issue and allow your Java application to run successfully.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值