JavaScript work must know (eight) attribute value, write, enumerate, configurable

Source: Internet
Author: User

Attributes of a property

Each object has properties, what attributes do they have?

   1, the attribute has a value.

2, whether the property is writable.

3, whether it is enumerable.

4, whether it is configurable.

 "Use strict";//using strict mode var descriptor = Object.getownpropertydescriptor ({x:1}, "X");//Get the value of an anonymous object alert ("Val UE: "+ descriptor.value +" \ r \ n writeable: "+ descriptor.writable +" \ r \ n enumberable: "+ descriptor.enumerable +" \ r \ n Co Nfigurable: "+ descriptor.configurable);//value:1 Writeable:true enumberable:true,configurable:true var BaseDescri        Ptor = object.getprototypeof ({x:2});        For (var p in Basedescriptor) {alert (P);//Built-in properties cannot be enumerated.        } var o = {};        Object.defineproperty (o, "Y", {value:1, writable:true, Enumberable:false, configurable:true});//sets properties of attributes.               var odescriptor = Object.getownpropertydescriptor (o, "Y");            Object.defineproperties (o, {x: {value:3, Writable:false, Enumberable:true, configurable:true},        Z: {get:function () {return this.x + 1;}, Enumerable:true, Configurable:true}//get represents a read-only property, set can design write properties.        });        alert (O.Y); Alert ("\ r \ n Value:" +Odescriptor.value + "\r\nwritable:" + odescriptor.writable + "\ r \ n enumberable:" + odescriptor.enumerable + "\ r \ n Configu        Rable: "+ odescriptor.configurable);        alert (O.Z);        o.x = 4; alert (O.Z);

  

The modification of a property follows the following rules:

if the object is not extensible, grams edits an existing own property, but cannot add a new attribute to it.

If the object is not configurable, you cannot modify his or her configurable and enumerable nature.

If the accessor property is not configurable, its getter and setter methods cannot be modified, nor can it be converted to a data property.

If the data property is not configurable, it cannot be converted to an accessor property.

If the data property is not configurable, you cannot modify its writeable from false to true, but you can modify it from true to false.

If the data property is not configurable and is not writable, its value cannot be modified, but the value of the configurable but non-writable property can be modified. (actually mark him as writable, then modify his value, and finally convert it to non-writable.)

Summary

property, you need to continue the study before you can ...

JavaScript work must know (eight) attribute value, write, enumerate, configurable

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.