Object (the only complex type)

I. Definition:

Unordered collection of data

The set of structural pairs

Two, writing method:

  • Easy to write:let obj = { 'name': 'frank', 'age': 18 }
  • Formal writing:let obj = new Object({'name': 'frank'})
  • Written anonymously:console.log({ 'name': 'frank, 'age': 18 })
Note:
  • The key name is a string!
  • Key names are strings, not identifiers, and can contain any character
  • Quotation marks can be omitted, after which only identifiers can be written
  • The rendering quotes are omitted and the key name is still a string (important)

Properties of objects

  • Property name: Each key is the property name of the object.
  • Property value: Each value is the property value of the object

1. Strange attribute names:

All attribute names are automatically changed to strings

let obj = {
  1: 'a'.3.2: 'b'.1e2: true.1e-2: true.234.: true.0xFF: true
};
/** Key values for each of the above attributes: 1 is '1' 3.2 is '3.2' 1e2 is '100' 1e-2 is '0.01'.234 is '0.234' 0xFF is '255'Copy the code

Keys (obj) to obtain all keys of obj

2, variable as attribute name:

How do I use variables for attribute names? It used to be constant property names

let p1 = 'name'
let obj = { p1 : 'frank'} // Write it like this, property name is 'p1'
let obj = { [p1] : 'frank' } // The property name is 'name'
Copy the code
  • Attribute names without [] are automatically changed to strings.
  • [] is evaluated as a variable.
  • A value that is not a string automatically becomes a string

3. Object hidden properties:

  • Every object in JS has a hidden attribute __proto__
  • This hidden property stores the addresses of objects whose shared properties are made up of
  • The window.object. prototype Object is called the prototype, also known as the root of the Object
  • That is, the hidden attribute __proto__ of each object holds the address of the window.object. Prototype
  • Every object has a prototype
  • A prototype is an object, so there’s a prototype, but the prototype is null.
var obj= {}obj.toString() // No error
// Because obj's hidden property corresponds to an object with toString()
Copy the code

4. Add, delete, change and check object attributes

1. Delete attributes

XXX or obj[‘ XXX ‘] : I therefore pay no attention to the difference: OBj. XXX = Undi25th the name of the property remains, but the value of the property will therefore be undi25

For example:

(1)

Note: Only ‘XXX’ in obj can be used to check whether the property name is still in the object: true indicates yes, false indicates no

(2)

‘XXX’ in obj && obj. XXX === undefined returns true, indicating that the attribute XXX is still in obj and its value is undefined

Note that obj. XXX === undefined does not determine whether ‘XXX’ is a property of obj.

2. Read properties

View all properties of an object:

  • To view all of an object’s own properties: Object.keys(obj)
  • To view all of an object’s own property values:Object.values(obj)
  • View all of an object’s own properties and values: just the object nameobJ orObject.entries(obj)
  • View self + Common properties:console.dir(obj)
  • View common properties: use them in turnObject.keysPrint out theobj.__ proto_
  • To determine whether an attribute is self-contained or common:Obj. hasOwnProperty(' Property name ')(Returning true indicates that the property is its own property, false indicates that the property is shared)
  • 'key' in objCheck to see if the property name is still in the object: true means yes, false means no

View a property of an object

  • Bracketed syntax: obj['key']orobj['k'+'ey']
  • Some grammar:obj.key
  • Pit new grammar:obj[key]// The value of variable key is usually not ‘key’.

For example,

let list = ['name'.'age'.'gender']
let person = {
    name: 'yy'.age : 18.gender : 'woman'
}
for (let i = 0; i < list.length; i++) {
    let name = list [i]
    console.log(person.name)  / / the key
}
// the result is yy*3, the first attribute of person
Copy the code
let list = ['name'.'age'.'gender']
let person = {
    name: 'yy'.age : 18.gender : 'woman'
}
for (let i = 0; i < list.length; i++) {
    let name = list [i]
    console.log(person[name]) / / the key
}
// result is yy, 18, woman of person
Copy the code

3. Write properties

1. Modify or add attributes

(1) Direct assignment

let obj = {name: 'frank'} // Name is a string
obj.name = 'frank' // Name is a string
obj['name'] = 'frank'
***obj[name] = 'frank' // Error, because name value is not determined
obj['na'+'me'] = 'frank'
let key = 'name'; obj[key] = 'frank'
let key = 'name'; ***obj.key = 'frank'~ ~// error because obj. Key is equivalent to obj['key']
Copy the code

(2) Batch assignment

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

2. Modify or add public attributes

Common attributes cannot be modified or added by themselves

  • Let obj = {}, obj2 = {} have toString
  • Obj. ToString = ‘XXX’ will only change the obj property
  • Obj2.tostring is still a prototype

I want to change or add attributes to the prototype

  • Obj.__proto__. ToString = ‘XXX’ // __proto__ is not recommended

  • Object.prototype.toString = 'xxx'

  • In general, don’t modify a prototype, it can cause a lot of problems

3. Modify hidden attributes

__proto__ is not recommended

let obj = {name:'frank'}
let obj2 = {name: 'jack'}
let common = {kind: 'human'}
obj.__proto__ = common
obj2.__proto__ = common

Copy the code

Object. Create is recommended

let obj = Object.create(common)
obj.name = 'frank'
let obj2 = Object.create(common)
obj2.name = 'jack'
Copy the code

Norms basically mean, if you want to change, change at the beginning, don’t change later

This chapter summarizes

delete

delete obj['name']
'name' in obj // false
obj.hasOwnProperty('name')  // false
Copy the code

check

Object.keys(obj)
console.dir(obj)
obj['name']
obj.name // Remember that name is a string
obj[name]  // Remember that name is a variable
Copy the code

change

  • To change their ownobj['name'] = 'jack'
  • Batch self modificationObject.assign(obj, {age:18, ... })
  • Modified common propertyobj.__proto__['toString'] = 'xxx'
  • Modified common propertyObject.prototype['toString'] = 'xxx'
  • To change the prototypeobj.__proto__ = common
  • To change the prototypelet obj = Object.create(common)

Note: All __proto__ code is strongly discouraged

increase

  • Basically the same as above: existing attributes are changed; Increment without attribute