current position:Home>Basic usage of JS object

Basic usage of JS object

2021-08-27 11:21:21 u675c

Two kinds of syntax for declaring objects

object Definition

An unordered collection of data Set of key value pairs

grammar

let obj = { 'name': 'tarro', 'age': 18}

let obj = new Object({'name': 'tarro'})
 Copy code 

console.log({'name': 'tarro', 'age': 18})

details :

The key name is a string , It's not an identifier , Can contain any character

Quotation marks can be omitted , Only identifiers can be written after omission , But the key name is also a string

Property name —— Every key Are the attribute names of objects (property)

Property value —— Every value Are the attribute values of the object

Delete object properties

How to write it

delete obj.xxx or delete obj['xxx']—— Delete obj Of xxx attribute

Be careful :obj.xxx = {undefined}—— This way of writing can only obj Of xxx Change attribute value to null , But the property name is still

Property name not included

'xxx' in obj => false—— Judge 'xxx' Is it obj Properties of ( But it is not judged whether it is a self attribute or a common attribute )

Contains the attribute name , But the value is indefined

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

Be careful obj.xxx === undefined It's impossible to judge 'xxx' Is it obj Properties of , Only use 'xxx' in obj

View the properties of an object

See all the properties

View all of its properties

Object.keys(obj)

Look at yourself + Common property ( Prototype )

  • console.dir(obj)
  • Or use it yourself once Object.keys Print out obj.__proto__

image.png

Determine whether an attribute is self or common

obj.hasOwnProperty('toString( This attribute )')

View the properties

  • Bracket syntax :obj['key']
  • Some grammar :obj.key
  • Pit new grammar :obj[key]   // Variable key The value is generally not 'key'

tips: Please use parenthesis syntax first ; A little grammar will mislead you , Make you think key It's not a string ; Make sure you don't confuse the two grammars , Use a little more Syntax .

Syllogism logic

Every object has a prototype

  • There are common properties of objects in the prototype
  • such as obj The prototype of is an object
  • Obj.__proto__ There is the address of this object
  • This object has toString/constructor/valueOf Equal attribute

The prototype of an object is also an object

  • So the prototype of an object also has a prototype
  • obj={} The prototype of is the prototype of all objects
  • This prototype contains the common properties of all objects , Is the root of the object
  • This prototype also has a prototype , yes null—— Artificially designated ( Complex may also specify an address )

namely : Every object has a prototype ; The prototype of an object is also an object ; This prototype also has a prototype , yes null.

example : Distinguish variables name And constant strings 'name'

image.png choose 2( Print all properties , If it is 1 Only name)

analysis

image.png

image.png

Modify or add the properties of the object

Direct assignment

If there is one, change it , Nothing increases

let obj = {name: 'tarro'}    name Is string
obj.name = 'tarro'   name Is string
obj['name'] = 'tarro'  
obj[name] = 'tarro' wrong , because name Value uncertainty , Not necessarily a string
obj['na'+'me'] =  'tarro'  
let key = 'name'; obj[key] = 'tarro' key It's a variable. ,key='name', So variable key='tarro'
let key = 'tarro'; obj.key = 'tarro' wrong , because obj.key Equivalent to obj['key']

Batch assignment

  • Object.assign(obj, {age:18, gender:'man'})
  • Added in obj In the array , Before obj Keep the original content in

Modify or add common attributes

You cannot modify or add shared properties by yourself

  • let obj = {}, obj2 = {} // Prototype toString
  • obj.toString = ‘xxx’ Will only change in obj On their own properties
  • obj2.toString Still on the prototype

Modify or add attributes on the prototype

obj.__proto__.toString = 'xxx'   // Not recommended __proto__( You can use the following )
Object.prototype.toString = 'xxx'   But generally don't modify the prototype , Can cause bug Of

Modify hidden properties

It is not recommended to use __proto__ image.png Recommended Object.create()     image.png

Object.create( attribute , There is also a property that can be assigned directly, which is more troublesome ), Example : image.png

'name' in obj and obj.hasOwnProperty('name') The difference between

'name' in obj //'name' yes obj Properties of true, Not so false obj.hasOwnProperty('name') // Determine whether it is its own attribute . it is then true, Not so false

copyright notice
author[u675c],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827112116791t.html

Random recommended