Go A new property method for ECMAScript5 object

Source: Internet
Author: User

Although not all browsers have now supported the new features of ECMASCRIPT5, but compared to ECMAScript4 ECMAScript5 by the vast number of browser manufacturers widely accepted, the current mainstream browser only the low version of IE is not supported, Others have more or less supported the new features of ECMAScript5, among which the most natural is the base type of all objects--object

Object.create (Prototype[,descriptors])

This method is used to create an object, assign its prototype property to the first parameter, and can set multiple descriptors, about Decriptor the next method will be described here first not to say. That's all you need to create a prototype chain clean object.

var o = object.create ({            "say": function () {                alert (this.name);            },            "name": "Byron"        });

Object.defineproperty (O,prop,descriptor)/object.defineproperties (o,descriptors)

To understand that these two functions must understand what descriptor is, the Object field in the previous JavaScript is an object property, a key-value pair, and a few features introduced in the ECMASCRIPT5 Property,property

1. Value: values, default is undefined

2. Writable: Read-only property, default is False, a bit like the const in C #

3. Enumerable: Whether it can be enumerated (for in), false by default

4. Configurable: Can be deleted, default false

Same as C #, Java Get/set, but these two can not be used in conjunction with value, writable

5.get: A worthwhile way to return the property, default is undefined

6.set: The method of setting a value for the property, default is undefined

Object.defineproperty (o, ' age ', {            value:24,            writable:true,            enumerable:true,            configurable:true        });        Object.defineproperty (o, ' sex ', {            value: ' Male ',            writable:false,            enumerable:false,            configurable: False        });        Console.log (O.age);        o.age = +;        for (Var obj in o) {            console.log (obj + ': ' + o[obj]);            /*            age:25  //No Sex:male traversed            say:function () {                alert (this.name);            }             Name:byron             */        }        delete o.age;        Console.log (o.age);//undefined         Console.log (o.sex);//male        //o.sex = ' female ';//cannot assign to read only Property ' sex ' of #<object>         delete o.age;         Console.log (O.sex); Male, and has not been removed

You can also use the Defineproperties method to define multiple property at the same time

Object.defineproperties (o, {            ' age ': {                value:24,                writable:true,                enumerable:true,                configurable : True            },            ' sex ': {                value: ' Male ',                writable:false,                enumerable:false,                configurable:false            }        });
Object.getownpropertydescriptor (O,property)

This method is used to obtain the property attribute of the DefineProperty method setting

var props = Object.getownpropertydescriptor (o, ' age ');        Console.log (props); Object {value:24, writable:true, Enumerable:true, configurable:true}

Gets all the property names, not including the properties in Prototy, and returns an array

Console.log (Object.getownpropertynames (o)); ["Age", "sex"]

In the example, you can see that the Name property in prototype is not getting

Object.keys ()

Similar to the Getownpropertynames method, but takes all enumerable properties and returns an array

Console.log (Object.keys (o)); ["Age"]

The above example shows that non-enumerable sex is not getting

Object.preventextensions (O)/object.isextensible

The Object.isextensible method is used to lock the object's properties so that it cannot be extended, that is, the new property cannot be added, but the value of the property can still be changed, or the property can be deleted, and the object can be used to determine whether the objects may be expanded

Console.log (Object.isextensible (o)); True        o.lastname = ' Sun ';        Console.log (O.lastname); Sun, at this time the object can expand        object.preventextensions (o);        Console.log (Object.isextensible (o)); False        O.lastname = "Byronsun";        Console.log (O.lastname); Byronsun, the attribute value can still be modified        //delete o.lastname;        Console.log (O.lastname); Undefined can still delete properties         o.firstname = ' Byron ';//can ' t add property FirstName, object was not extensible could not add attribute
Object.seal (O)/object.issealed

method is used to seal the object, that is, to let the object can neither expand nor delete the attributes (set the configurable of each property to false), the singular attribute value can still be modified, object.issealed due to determine whether the object is sealed

Object.seal (o);        O.age = 25; Delete O.age can still be modified        ;//cannot Delete Property ' age ' of #<object>
Object.freeze (O)/Object.isfrozen

The ultimate artifact, a completely frozen object, cannot be modified on the basis of seal (the wirtable of each property is also set to false)

Object.freeze (o);        O.age = 25; Cannot assign to read "age" of #<object>
At last

The code above is run in the next strict mode (' use strict ') in Chrome 29, and the method mentioned is a static function of object, that is, it should be object.xxx (x) when used, and cannot be invoked as an object instance. In general, ES5 adds these methods to provide further configuration for JavaScript object-oriented design, which feels good to use.



Go A new property method for ECMAScript5 object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.