define

An unordered collection of attributes (hash table) whose attributes can contain base values, objects, or functions;

Each object is created based on a reference type, which can be a native or a custom type.

Create an object

Create an Object instance

    var person = new Object()
    
    person.name = 'Memory'
    person.age = 24
                
    person.sayName = function () {
        console.log(this.name)  // Memory
    }
Copy the code

Object literals

    var person = {
        name: 'Memory'.age: 24.sayName: function () {
            console.log(this.name)  // Memory}}Copy the code

Where name and age are the attributes of Person, and sayName is the person method.

Attribute types

Properties that are internal only and cannot be accessed directly in JS are placed in [[]] to indicate that properties are internal values;

ECMAScript has two types of properties, data properties and accessor properties.

Data attributes

The [[Writable]], [[Enumerable]] and [[Writable]] of person.name are both true, and [[Value]] represents Memory.

Modify data attributes: Object.defineProperty(Object obj, [property name], descriptor Object)

The properties of the descriptor object must be different, enumerable, writable, and value.

DefineProperty, the default values of Enumerable and writable, if not specified, are false.

The same property can be modified by calling Object.defineProperty() multiple times before setting the configured information to false. And then I’m going to dividewritableAnything else will cause errors;

Try modifying writable:

    Object.defineProperty(person, 'name', {
        configurable: false,})console.log(person.name)   // Memory
    
    Object.defineProperty(person, 'name', {
        writable: true.value: 'doublemeng'
    })
    
    console.log(person.name)   // doublemeng
Copy the code

Modify 64x:

    Object.defineProperty(person, 'name', {
        configurable: false,})console.log(person.name)   // Memory
    
    // Uncaught TypeError: Cannot redefine property: name
    Object.defineProperty(person, 'name', {
        configurable: true.value: 'doublemeng'
    })
    
    console.log(person.name)   
Copy the code

If the 64x is set to false, the property cannot be deleted from the object.

If this object is called delete, it will be ignored in non-strict mode and thrown in strict mode.

If writable is set to false, the property is not writable.

It is ignored in non-strict mode and thrown incorrectly in strict mode.

Accessor properties

Accessor properties contain getter and setter functions (neither of which are required), where the setter is used to write the value and the getter is used to read the value.

Setting the value of one attribute causes other attributes to change, as in the following example:

    var book = {
        age: 1._year: 2000   // Attributes that can only be accessed through object methods
    }
    
    console.log(book._year)   / / 2000
    console.log(book.age)   / / 1
    
    Object.defineProperty(book, 'year', {
        get: function () {
            return this._year
        },
        set: function (newYear) {
            this._year = newYear
            this.age = newYear - 2000 + 1
        }
    })
    
    book.year = 2019
    
    console.log(book._year)   / / 2019
    console.log(book.age)   / / 20
Copy the code

You don’t have to specify getters and setters;

Specifying only getters means that properties can only be read, not written;

Attempts to write are ignored in non-strict mode and thrown incorrectly in strict mode.

Specifying setter only means that the property can be read but not read;

An attempt to read will return undefined in non-strict mode or throw an error in strict mode.

Prior to this method, two non-standard methods were used to create accessor properties:__defineGetter__()__defineSetter__()

    var book = {
        age: 1._year: 2000   // Attributes that can only be accessed through object methods
    }
    
    console.log(book._year)   / / 2000
    console.log(book.age)   / / 1
    
    book.__defineGetter__('year'.function () {
        return this._year
    })
    
    book.__defineSetter__('year'.function (newYear) {
        this._year = newYear
        this.age = newYear - 2000 + 1
    })
    
    book.year = 2019  
    
    console.log(book._year)   / / 2019
    console.log(book.age)   / / 20
Copy the code

Does not supportObject.defineProperty()The browser cannot be modified[Configurable]]and[[Enumerable]]

Defining multiple properties

Object.defineproperties (Object obj, whose attributes are the attributes obj wants to add and modify)

Read properties of properties

Object. GetOwnPropertyDescriptor (Object, property)

reference

JavaScript Advanced Programming

summary

This article mainly introduces various features of object attributes. It provides [[Enumerable]], [[Writable]], [[Value]] and [[Enumerable], [[Get]], [[Set]] with different data properties.

If you have any questions, please point out.