Use of the JavaScript object class method

Source: Internet
Author: User

1. Get the property name

var o = {p1:123, p2:456}; Object.keys (o);//Get an enumerable property name//["P1", "P2"] Object.getownpropertynames (o); Get all property names//["P1", "P2"]

2. Get the information description of the attribute

var o = {p: ' a '};           Object.getownpropertydescriptor (o, ' P ')//Object {value: "A", Writable:true, Enumerable:true, Configurable:true}

3. Define a property

Object.defineproperty (Object, PropertyName, Attributesobject)

The first parameter is the target object, the second argument is the property name, and the third is the property description

Var o = object.defineproperty ({},  "P", {                              value: 123,                              writable :  false, //can write                              enumerable: true,                               configurable: false });                       o.p // 123                      o.p = 246;                      o.p // 123 //  because writable is false, So the value of this property cannot be changed

Define multiple properties

Var o = object.defineproperties ({}, {                                                  p1: { value: 123, enumerable: true },                                                        p2: { value:  "ABC ", enumerable: true },                                                        p3: {                                                        //get: function ()  { return this.p1+this.p2 },                                                        VALUE: THIS.P1+THIS.P2,                                                                Whether        enumerable:true,//can be enumerated                                                                     configurable:true //Property Description can be modified                                                           }                                           });  o.p1 // 123 o.p2 //   "abc"  o.p3 //  "123abc"

Note that get cannot be present at the same time as value or writable to describe unified properties


4. Check whether the property is enumerable

Object.prototype.propertyIsEnumerable () user.propertyisenumerable (' toString ');


5. Prohibit dynamic expansion of objects

Object.preventextensions (object);
var o = new Object (); Object.isextensible (o)//True Object.preventextensions (O); Object.isextensible (o)

6. Freeze the object so that it becomes a constant object (cannot extend properties and methods)

var o = {p: "Hello"}; Object.freeze (o); O.P = "World"; O.P//Hello o.t = "Hello"; O.T//undefined


Use of the JavaScript object class method

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.