Today summarize a big mountain about JS, that is THE OBJECT of JS.

The definition of an object can be:

  • Unordered collection of data
  • A collection of key-value pairs

How to write a declared object

Declaration objects can be written like this:

let obj = { 'name':'frank','age': 18 }; // let obj = new Object({'name':'frank'}); Console. log({'name':'frank','age': 18})Copy the code

Where, ‘name’:’ Frank ‘is the property of object obj, and name is the key of object, namely the property name. Frank is the value of the object, that is, the attribute value.

A few other details are worth noting:

  • Key names are strings, not identifiers, and can contain any character;
  • Quotes can be omitted, but only identifiers can be written, and the key remains a string.

When using a variable as the attribute name, use [] to evaluate the variable as the attribute name. Otherwise, the attribute name will automatically become a string, for example:

let p1 = 'name'; Let obj = {p1: 'frank'} // write property 'p1'; Let obj = {[p1]:'frank'}Copy the code

Delete object property notation

To delete an object attribute, use:

XXX or delete obj[' XXX '] // Obj [' XXX '] // obj = undefined // only delete XXXCopy the code

Using DELETE directly deletes the entire XXX property of the object, including the property name and value.

To check whether the XXX property is in the object, you can execute:

'XXX' in obj. // 'XXX' in obj&&obj. XXX === undefined // The object contains the property name, but the value is undefinedCopy the code

Then check whether the return value is true or false to determine whether it is in the object. However, you cannot determine whether the property is in the object or hidden property of the object. To determine exactly, you need to execute the following code:

obj.hasOwnProperty('xxx')
Copy the code

View object properties

To view the properties of an object, use:

Keys (obj) console.dir(obj) // View all properties of its own + common property obj.hasOwnProperty('toString') // Determine whether the property is owned or sharedCopy the code

Obj. Name = obj[‘name’]; obj. Name = obj[name]; Because name in obj. Name represents a string, and [name] in obj[name] represents the name variable.

Modify or add object property notation

Modifying and adding attributes are the same in the command. The modification is just a rewriting process, which is written as follows:

let obj = {name: Obj ['name'] = 'frank' //name is a string obj['name'] = 'frank' //❌, Obj ['na'+'me'] = 'frank' // obj[key] = 'frank' let key = 'name'; Obj. key = 'frank' //❌, obj.key = obj['key']Copy the code

In addition to modifying its own attributes one by one, you can also directly batch assign values:

Object.assign(obj,{age:18,gender:'man'})
Copy the code

At the same time, JS can also modify the common attributes, but cannot modify or increase the common attributes by itself:

Let obj = {},obj2 = {} // total toString obj. ToString = 'XXX' // will only modify obj's own property obj2.toString //obj2's toString is still on its prototypeCopy the code

If you must modify or add attributes to the prototype, use:

let common = {'a':1}
let obj = Object.create(common)
obj.name = 'frank'
let obj2 = Object.create(common)
obj2.name = 'jack'
Copy the code

You get the following results:

Pay attention to the point

There is one other notable difference between ‘name’ in obj and obj.hasownProperty (‘name’).

The former is used to determine whether the object obj contains the name attribute.

The latter is used to determine whether the name in the object belongs to the object itself or to the prototype of the object. If it is not clear, draw a JS memory map to help understand.

The above is the summary of the basic usage of JS objects that I currently master.

© This summary course copyright belongs to the author, reprint need to indicate the source