JavaScript Learning Notes-Object-oriented understanding objects

Source: Internet
Author: User

There are two types of properties in ECMAScript: Data properties and accessor properties

One attribute type

1. Data properties. Data attributes have 4 properties that describe their behavior

[[Configurable]] Indicates whether a property can be redefined by deleting the property, whether the property can be modified, or whether the property can be modified as an accessor property.

[[Enumerable]] Indicates whether an enumeration can be performed through a for-in loop.

[[writable]] Indicates whether the value of the property can be modified

[[value]] contains the value of this property. Read and write property values, all from this location

The properties that are defined when the object literal is initialized, and these attributes are true by default.

Object.defineproperty (object, property, descriptor)//descriptor called Descriptor object

var person ="Name", {    false,  // Set object property to property value cannot be modified    // initializes the value of the property name. Because the value attribute is included, the "name" property is a numeric attribute  //  Nicholasperson.name =  "Greg"   Nicholas

[[Configurable]] attribute if set to False, you cannot use Delete to delete this property. And can only modify writable this feature, the other properties will no longer be modified.

2. Accessor Properties

Accessor properties do not contain data values. properties consist of [[configurable]] and [[Enumerable]]. [[Get] and [[Set]] properties are not required.

The [[Get]] and [[Set]] properties are similar to the magic methods in PHP.

varBook ={_year:2004, Edition:1};object.defineproperty (book,"Year", {//because the get and set attributes are set, the year property is an accessor property. No more value,writable featuresGetfunction(){         return  This. _year; }, set:function(newvalue) {if(NewValue > 2004){                 This. _year =NewValue;  This. edition + = newValue-2004; }}); Book.year= 2005; alert (book.edition);//2

Specifying a getter only means that the property is not writable. Specifying only the setter means that the property is unreadable.

Browsers that do not support the Object.defineproperty () method use non-standard methods:

Obj.__definegetter__ and obj.__definesetter__

Browsers that do not support object.defineproperty () cannot modify [[configurable]],[[enumerable]] these two features

Two defining multiple properties

Object.defineproperties () define multiple properties

var book;object.defineproperties (book,{    2004},    // Data Property    Edition: {value:1},        // Data attribute year    : {      // accessor property          function() {                 return  This . _year;          },           function (){                ....          }    } });

Properties of three read properties

Object.getownpropertydescriptor (Obj,attributename)

Obj--The object where the property resides, AttributeName--the name of the property whose descriptor is to be read. Returns an object that returns the values of the different 4 attributes, depending on the type of the property (data property, accessor property).

  

  

JavaScript Learning Notes-Object-oriented understanding objects

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.