JavaScript object-oriented programming record notes

Source: Internet
Author: User

The following is the JavaScript Advanced programming sixth chapter object-oriented program design reading notes records.

There are two properties for an object: Data property/Accessor property.

Data properties include four values: configurable: The default value is True, which indicates whether the Delete property can be redefined by deleting it, whether the property attribute can be modified, or whether the property can be modified as an accessor property.

Enumable: Indicates whether the property can be returned through a for in loop. The default value is true.

Writable: Indicates whether the property can be modified, the default value is true.

Value: The data value that contains this property.

Accessor properties include four values: configurable: The default value is True, which indicates whether the property can be redefined with the delete delete property, whether the property attribute can be modified, or whether it can be modified as a data property.

Enumable: Indicates whether the property can be returned through a for in loop, and the default value is true.

Get: The function that is called when the property is read, the default value is undefined.

Set: The function that is called when the property is written, the default value is undefined.

This involves methods such as Object.defineproperty ()/object.defineproperties ()/object.getownpropertydescriptor ().

  Object.defineproperty (), pass in three values, the first is the object that defines the property, the second is the object property that defines the property, and the third is the property descriptor. As follows:

var obj = {};

Object.defineproperty (obj, ' name ', {value: ' Tom '});

  object.defineproperties (), pass in two values, define multiple attribute attributes, the first is the object that defines the property, and the second is the property health value pair that defines the attribute attribute, as follows:

var obj = {};

Object.defineproperties (obj, {

_year: {value:123}, //attribute added ' _ ' prefix, indicating that the property can only be accessed through the object's accessor property, which is the Get ()/set () method.

Name: {value: ' Tom '},

Year: {

Get:function () {

return this._year;

}

}

});

Object.getownpropertydesciptor (), which represents the descriptor that gets a property (the data property or accessor property) that passes in two arguments, the first representing the object that contains the property, the second property name that represents the descriptor, and the return value as an object. As follows:

Referring to the Obj object defined above, call Object.getownpropertydescrptor ().

var descriptor = object.getownpropertydescriptor (obj, _year);

Console.log (Descriptor.value); Output 123

Console.log (Descriptor.get); Output undefined

var Descriptor2 = object.getownpropertydescriptor (obj, year);

Console.log (Descriptor.value); Output undefined

Console.log (Descriptor.get); Output function

    

JavaScript object-oriented programming record notes

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.