Two syntax for declaring objects

  • The first is the easy way to write it
let obj = {
    'name' : 'xxx'.'age' : 18
}
Copy the code
  • The second is primitive grammar
let obj = new Object({
    'name' : 'xxx'.'age' : '18'
})
Copy the code

Both syntax can be used, but the simple first is often used.

Pay attention to

  • Objects appear as key-value pairs
  • The key name is a string, not an identifier, and can contain any string
  • Quotation marks can be omitted, and only identifiers (excluding numbers) can be written after omission.
  • Even if the quotes are omitted, the key is still a string
  • If the key name has special characters such as Spaces or Chinese characters, quotation marks must be used. Otherwise, errors will occur
  • There are no numeric keys and no numeric subscripts because the browser automatically converts numbers to strings

Add, delete, change and check objects

Delete attributes

Delete obj. XXX or delete obj[‘ XXX ‘]

  • Delete the XXX property of obj
  • Please distinguish between “attribute value undefined” and “without attribute name”.

Determine whether an attribute is self-contained or common

obj.hasOwnProperty('toString')

How do I know if something is in an object as a property name?

  • Without attribute name:'xxx' in obj === false
  • Contains the attribute name, but the value isundefined

'xxx' in obj && obj.xxx === undefined

  • Pay attention toobj.xxx === undefinedCan’t tell'xxx' Whether it isobjProperties.
let obj = {}

Copy the code

'xxx' in obj andobj.hasOwnPropertyThe difference between

In does not distinguish between properties that are self – or common.

obj.hasOwnProperty('toString')  //false indicates that obj does not have toString and is a common attribute of the stereotype.
Copy the code

View all properties

  • View all of its properties

Object. Keys (obj) allows you to query all keys

Object. Value (obj) Can query all values

Object.entries(obj) can look up all keys and values

  • View self + Common properties

The console. Dir (obj);

Or print out obj.__proto__ in order with object. keys yourself

View a property

  • There are two ways to view properties
  1. Bracketed syntax:obj['key']
  2. Some grammar:obj.key

Obj [key] // where key is a variable; Obj. Key The key is a string.

Use parentheses in preference. Dot syntax will mislead you into thinking key is not a string. Switch to dot syntax when you are sure you can’t confuse the two.

The prototype

  • Every object has a prototype

The stereotype holds the common properties of the object

For example, the prototype of OBj is an object

Obj.__ proto__ holds the address of this object

This object has properties such as toString/ constructor/valueOf

  • The prototype of an object is also an object

So the prototype of an object also has a prototype

The stereotype of obj = {} is the stereotype of all objects

This stereotype contains properties common to all objects and is the root of the object

This prototype also has a prototype, which is NULL

  • Relationships between archetypes and common attributes

A stereotype is an object that contains all the common properties, or the common properties are attached to the stereotype object.

Modify or add attributes

  • Direct assignment
let obj = {name: 'XXX'} // Name is a string
obj.name = 'XXX' // Name is a string
obj['name'] = 'XXX' 
obj['na'+'me'] = 'XXX'
let key = 'name'; obj[key] = 'XXX'
Copy the code
  • Batch assignment
Object.assign(obj, {age: 18.gender: 'man'. })Copy the code

Modify or add a common property

  • Common attributes cannot be modified or added by themselves
let obj = {}, obj2 = {} / / the toString
obj.toString = 'xxx' // Only obj attributes will be changed
obj2.toString // Still on the prototype
Copy the code
  • Forcibly modify or add attributes to the stereotype
obj.__proto__.toString = 'xxx' // __proto__ is not recommended
Object.prototype.toString = 'xxx' 
Copy the code
  • In general, don’t modify a prototype, it can cause a lot of problems

Modify hidden attributes (stereotypes)

  • Using __ proto__ is not recommended
let obj = {name:'XXX'}
let obj2 = {name: 'YYY'}
let common = {kind: 'human'}
obj.__proto__ = common
obj2.__proto__ = common
Copy the code
  • Object. Create, a new API in ES6, is recommended
let obj = Object.create(common)
obj.name = 'XXX'
let obj2 = Object.create(common)
obj2.name = 'YYY'
Copy the code

If you want to change, change at the beginning, don’t change later.

Modify and add belong to write, check belongs to read, check will look at the prototype chain, write do not look at the object prototype chain, can only change or add its own attributes.

All “proto” code is strongly discouraged