The second edition of JavaScript Advanced Programming understands objects

Source: Internet
Author: User

Statement: This category belongs to the study notes, mainly extracts the contents of the book, less collation. Content is often jumping, it is recommended that you read books, the benefits are greater.

ECMA-262 defines an object as "a collection of unordered attributes whose properties can contain basic values, objects, or functions"

Object is a set of values that do not have a specific order, each property or method has a name, and each name maps a value.

Each object is created from a reference type, which can be either the native type discussed in Chapter 5th or a developer-defined type

The usual way to create an object is to create an instance of it with the object built into it, and then add properties and methods to it.

var person=New  Object ();p erson.name= "Nico";p erson.age=22;p erson.sayname =function() {     alert (this. Name)  }

Later, people gradually used object literals to create objects

var person={    name:"Nico", age    :$,    sayname:function  () {         alert (this. Name)     }};

Property

The various characteristics of attributes are described when ECMA-262 version fifth defines features that are only used internally.

ECMA-262 defines these features for use by the JavaScript engine, so they cannot be accessed directly in JavaScript.

To represent internal values, the specification places them in two opposing brackets, for example [[Enumerable]]

Data properties: Contains a data worthy location, where you can read and write values, there are 4 properties that describe their behavior

    • [[[Configurable]]: Indicates whether the property can be redefined by deleting the property, modify the property's characteristics, directly define the property on the object, or modify it to the accessor property, which defaults to True
    • [[Enumerable]]: Ability to return properties through for-in loops, properties that can be directly defined on an object, by default True
    • [[[Writable]]: Indicates whether the value of the property can be modified, the property directly defined on the object, by default True
    • [[Value]]: Contains the data value for this property. When reading a property, read from this location, write the property, write it in this, the default is undefined.

If these attribute values are not set, it will be the default. One thing, to modify these, you must call the Object.defineproperty () method, accept three parameters

    • Object where the property resides
    • The name of the property
    • Descriptor object (must be one of the 4 1~4 described above)

As follows

var person={};object.defineproperty (person,"name", {    writable:false,    value:"Kobe"//Kobeperson.name= "Mike"//Kobe 

Then note that if you modify the configurable set false, then this property of this object can not be changed back, there will be an error. Except writable can be changed. And when not specified, these values are changed from the default of true to False. Fortunately, most of us do not need to use these attributes.

Accessor properties

Do not include data values: they contain a bunch of getter and setter functions (these two functions are not required). When the accessor property is read, the getter is called and the setter is called when writing.

    • [[[Configurable]]: Indicates whether the property can be redefined by deleting the property, modify the property's characteristics, directly define the property on the object, or modify it to the accessor property, which defaults to True
    • [[Enumerable]]: Ability to return properties through for-in loops, properties that can be directly defined on an object, by default True
    • [[Get]]: The function that is called when reading. Default is undefined
    • [[Set]]: function called at write time, default is undefined

The same is not directly accessible, must be accessed by the same function as above

varbook={_year:2004, Edition:1};object.defineproperty (book,"Year", {get:function(){        return  This. _year; }, set:function(newvalue) {if(newvalue>2004) {             This. _year=NewValue;  This. edition + = newValue-2004; }}); Book.year=2006; Console.log (book.edition);//3

You can not specify at the same time, if the other is not specified, it means that it is not available. For example, only a setter is made, which means that it is unreadable;

Prior to this, the accessor properties used _definegetter_ and _definesetter_, and the preceding object was replaced with a specific object, such as Book._definegetter_ (), 2 parameters, omitting the first argument before

Define multiple properties Object.defineproperties (), accept 2 object arguments, the first parameter is the object to add and modify the property, and the second corresponds to one by one of the first object added or modified by the Chinese herb

Attributes of the Read attribute: Object.getownpropertydescriptor (); Accepts 2 parameters, the object where the property resides, and the name of the property to be read, returns an object

The second edition of JavaScript Advanced Programming understands objects

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.