Based on the review

Seven data types

  • number
  • string
  • bool
  • symbol
  • null
  • undefined
  • object

Five falsy value

  • null
  • undefined
  • 0
  • NaN
  • “(empty string)

Object the object

The seventh data type, the only complex type

define

Unordered data set A collection of key-value pairs

{ 'key1': 'value1', 'key2': 'value2', }

writing

let obj = { ‘name’: ‘frank’, ‘age’: 18 } let obj = new Object({‘name’: ‘frank’}) console.log({ ‘name’: ‘frank, ‘age’: 18})

details

The key name is a string, it’s not an identifier, it can contain any character and the quotes can be omitted, and then you can only write an identifier and even if the quotes are omitted, the key is still a string.

Attribute values

Each value is an attribute value of the object

The property name

Each key is the property name of the object

All attribute names are automatically changed to strings

let obj = {
  1: 'a'.3.2: 'b'.1e2: true,
  1e-2: true,
  .234: true,
  0xFF: true
};
Object.keys(obj)
=> ["1"."100"."255"."3.2"."0.01"."0.234"]

Copy the code

details

Keys (obj) to obtain all keys of obj

How do I use variables for attribute names

Use variables for attribute names

  • let p1 = 'name'
  • let obj = { p1 : 'frank'}I’ll write it like this, property name is ‘p1’
  • let obj = { [p1] : 'frank' }I’ll write it like this, property name’ name’

contrast

  • Attribute names without [] are automatically changed to strings
  • [] is evaluated as a variable
  • A value that is not a string automatically becomes a string

Superdimensional knowledge (symbol)

In addition to strings, symbols can also be property names

let a = Symbol()

let obj = { [a]: 'Hello' }

It’s not useful right now, but it’s going to be useful for a long, long time when we’re going to learn about iteration

Object’s hidden properties

Hidden attribute

  • Every object in JS has a hidden property
  • This hidden property stores the addresses of objects whose shared properties are made up of
  • The object made up of these common attributes is called a stereotype
  • That is, the hidden property stores the address of the stereotype

Code sample

  • var obj = {}
  • Obj. ToString ()
  • Because of obj’s hidden propertiesCorresponding objectThere are toString()

The prototype

Every object has a prototype

The prototype holds the common properties of the object. For example, the prototype of obj is an object.__proto__ holds the address of the object and the object has properties like toString/constructor/valueOf

The prototype of an object is also an object

So the prototype of an object has a prototype and the prototype of obj = {} is the prototype of all objects and the prototype contains the common property of all objects, which is the root of the object and the prototype also has a prototype, which is NULL

Add and delete

We’re running into the word again, this time adding and deleting properties of objects

Delete the properties

delete

XXX or delete obj[‘ XXX ‘] to delete the XXX attribute of obj

Distinguish between “attribute value undefined” and “no attribute name”

Contains no attribute name

'xxx' in obj === false

Contains the attribute name, but the value isundefined

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

Pay attention to

Obj. XXX === undefined cannot determine whether ‘XXX’ is a property of obj

analogy

Do you have any toilet paper? A: does not contain // the attribute name B: does not contain // the attribute name, but the value is undefined

Did not have namely do not have, undefined is undefined, must distinguish

Read properties & write properties

View all of its properties

Object.keys(obj)

View self + Common properties

Console. dir(obj) or print obj.__proto__ with object. keys yourself

Determine whether an attribute is self-contained or common

obj.hasOwnProperty('toString')

View properties or assign values to properties

The two methods
  • Bracketed syntax:obj['key'] & obj['key']= xxx
  • Some grammar:obj.key&obj.key= xxx
  • Pit new grammar:Obj [key] // The variable key is usually not 'key'.

Use bracket syntax in preference

  • The dot syntax will trick you into thinking that key is not a string
  • When you’re sure you don’t confuse the two grammars, switch to grammar

Focus on

Name is equivalent to obj[‘name’] obj. Name is not equivalent to obj[name] simply put, name is a string, not a variable

let name = 'frank'
obj[name]
Copy the code

The above code is equivalent to obj[‘frank’] rather than obj[‘name’] and obj.name

let list = ['name'.'age'.'gender']
let person = {
       name:'frank'.age:18.gender:'man'}
for(let i = 0; i < list.length; i++){
  let name = list[i]
  console.log(person.name)
}
/ / output
//frank
//frank
//frank
Copy the code
let list = ['name'.'age'.'gender']
let person = {
       name:'frank'.age:18.gender:'man'}
for(let i = 0; i < list.length; i++){
  let name = list[i]
  console.log(person[name])
}
/ / output
//frank
/ / 18
//man
Copy the code
let obj = {name: 'frank'} // Name is a string
obj.name = 'frank' // Name is a string
obj['name'] = 'frank'  / / for
obj[name] = 'frank' // Error, because name value is not determined
obj['na'+'me'] = 'frank'
let key = 'name'; obj[key] = 'frank'   / / for
let key = 'name'; obj.key = 'frank' / / wrongBecause obj. Key is equivalent to obj['key']
Copy the code

Batch assignment

Object.assign(obj, {name:'ccc',age: 18, gender: 'man'})

Modify or add a common property

Common attributes cannot be modified or added by themselves

let obj = {}, obj2 = {} / / the toString
obj.toString = 'xxx'Will only change the obj2. ToString property of obj itself, again on the prototypeCopy the code

I want to change or add attributes to the prototype

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

Modifying hidden attributes

Not recommended__proto__

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

It is recommended to useObject.create

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

conclusion

/ / delete
delete obj['name']
'name' in obj // false
obj.hasOwnProperty('name')  // false
/ / 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
/ / change
obj['name'] = 'jack' / / change itself
Object.assign(obj, {age:18. })// Change yourself in batches
obj.__proto__['toString'] = 'xxx'  // Change the common attributes
Object.prototype['toString'] = 'xxx'  // Change the common attributes
obj.__proto__ = common  / / change the prototype
let obj = Object.create(common)  / / change the prototype
// Note: All __proto__ code is strongly deprecated
/ / to add
// If you have an attribute, change it; Increment without attribute.
Copy the code

supplement

The difference between ‘name’ in obj and obj.hasOwnProperty(‘name’) is that there are two methods to check whether the property is in the object. In checks for hidden properties. HasOwnProperty does not return true for both its own properties and shared properties. Ownproperty returns true only for its Ownproperty.