Abstract:

  1. A statement object
  2. Deletes an attribute of an object
  3. View the properties of the object
  4. Modifies or adds attributes to an object

Declare objects

An object is a collection of key-value pairs.

1. Grammar

Contains the property name (key name) and the property value (key value).

1) Formal writing:let obj = new Object ({'name':'LLLLxto','age':9})

2.let obj = {'name':'LLLLxto','age':9}

3) In addition to:console.log ({'name':'LLLLxto','age':9})

2. About attribute names

1) Attribute names are strings, not other types, and can contain any character. Such as:

let obj={
    ' ':'a'
    1:'b'
    2.3:'c'
    456.:'d'
    1e-2:true
    0xFF:true
} 
Copy the code

Note: in ES6, the symbol type can also be used as an attribute name. 2) Key quotes can be omitted (without special characters). 3) Key names are strings even without quotes. 4) Use the value of the variable as the attribute name, enclosed in brackets []. Such as:

let a='xxx'
let obj={
    [a]:123
}
obj        / / return {XXX: 123}
Copy the code

Summary: Attribute names without [] are automatically changed to strings. Attribute names appended with [] are treated as variables, evaluated, and then converted to strings.

Delete the attributes of the object

1. Grammar:

1) Delete attribute name:delete obj.nameordelete obj['name']

2) Delete only attribute values:obj.name = undefined

2. Verify

1) Check whether the deletion is successful:'name' in obj, returns false to indicate that the attribute no longer exists

2) The attribute exists but the value is null:'name' in obj && obj.name === undefined

Note: obj.name === undefined because ‘name’ is not the attribute name of obj. Such as:

let obj={
    name:undefined
}
let obj2={}
obj.name === undefined   / / return true
obj2.name === undefined  // Also returns true, but in fact 'name' is not the property name of obj2
Copy the code

View the properties of the object

1. View all properties

1) View all its attribute names:Object.keys(obj)

2) View all its own attribute values:Object.values(obj)

3) View all attribute names and attribute values:Object.entries(obj)

4) View all its own attributes and common attributes (in the form of a directory) :console.dir(obj)

5) How to judge their own attributes/common attributes:obj.hasOwnProperty('name'), returns true indicating that it is its own property.

HasOwnProperty can only determine whether it is its own property, and can be combined with the IN operator to determine whether it is an inherited property (a shared property). Contrast: ‘name’ in obj does not distinguish between own attributes and common attributes

2. View specific properties

1)obj['name']

2)obj.name

  • Both methods are equivalent. Note that name is a string.
  • Wrong way:obj[name]In this case, name is a variable and the value of the variable is not determined.

Modify or add attributes to an object

Modify/add, the syntax is basically the same, have to change, do not add.

1. Modify or add attributes

1) Direct assignment

Let obj = {name:’LLLLxto’} ② obj. Name = ‘LLLLxto’ ③ obj[‘name’] = ‘LLLLxto’ ④

let key = 'name'
obj[key] = 'LLLLxto'
Copy the code

Wrong way:

let key = 'name'
obj.key = 'LLLLxto'
Copy the code

The key is to understand that an attribute name is a string, and that a variable’s attribute name is appended to [], evaluated in [], and then changed to a string.

2) Batch assignment

Object.assign(obj,{name:'LLLLxto',age:9})

2. Modify or add shared properties (not recommended)

JS Settings, cannot modify or add common properties by itself. If you must modify or add attributes of the stereotype (that is, common attributes), then:

1) Modify or add stereotype attributes

= ‘XXX’ (obj. __proto__. ToString (expressed in __proto__ Shared properties are not recommended). (2) the window Object. The prototype. ToString = ‘XXX’

2) Modify or add prototypes (essentially, modify or add objects)

① obj.__proto__ = ‘XXX’ (using __proto__ to indicate common attributes is not recommended) ② Add prototype, form prototype chain

let common = {age:9}
let obj = Object.create(common)
obj.name = 'LLLLxto'
Copy the code

This approach is recommended, which means that if you want to change the prototype, change it at the beginning, not later. Create an object and add an attribute

let obj = Object.create(common,{
    name: {value:'LLLLxto'}})Copy the code

Rather than

let obj = Object.create(common,{
    name:'LLLLxto'})/ / an error
Copy the code

Is not

let obj = Object.create({name:'LLLLxto'})  // The name attribute is added to the prototype, not obj's own attribute
Copy the code