– Es6 – Object
How do you instantiate it
- There are three steps through the new process
- Step 1 Create a new object
- The second step is to make the prototype of the object equal to the prototype of the function, meaning that the prototype of the object has the methods and properties of the prototype of the function
- The third step is to make the function this refer to the object, that is, the object itself has some properties and methods of the function instance
- The new construct method on Reflection can also instantiate a constructor. Its first argument is a constructor and its second argument is an array that takes arguments to constructors. Reflect is an API that operates on objects, porting methods on objects to it. Including some delete methods to delete the object key, making it a functional behavior.
Object Add key
- [key] : value;
- [] can write expressions;
- [] can be written to self-executing functions
let key = 5;
let obj = {
a:1,
b:2
[key]:3,
[true ? 'a' : 'b'],
[(function(){return 'aaaa')()] : 5
}
Copy the code
Accessor (GET /set)
//set must pass an argument //get must return a value let obj = {age: If (val === 1) {throw Error (' this attribute cannot be set ')}}, get age () {return 123}}Copy the code
++Reflect++
A new API for manipulating objects
Why Reflect?
- The internal language methods of Object are placed on Reflect. That is, the internal methods of Object are taken from Reflect, and Object and its methods are separated and placed on Reflect.
defineProperty
- The old writing
Object. DefineProperty (targer, property, attributes) / / returns an ObjectCopy the code
- The new writing
Reflect. DefineProperty (targer, property, attributes) / / returns a Boolean value of true/falseCopy the code
inmethods
- Used to check whether an attribute belongs to the object itself, the prototype chain
Object.hasOwnProperty()
- Checks if an attribute is an object itself, not the prototype chain
for in
- Not only through itself, but also through its prototype chain
- Get all enumerable properties, including stereotypes and those on the stereotype chain
Reflect. From the (obj, key)
- Returns a Boolean value true/false
- Check Obj for key
- The first value is the object
- The second value is key
Reflect.delectProperty(obj,key)
- Delete which key in which object
Reflect.construct(target, [args])
- Es6 instantiation function operations
- It was instantiated by new
- The first argument is the constructor
- The second argument is that the pass parameter must be an array
Reflect.apply(fn, obj, [])
- The first argument changes who this points to
- The second argument points to who
- The third parameter must be passed as an argument, or an empty array. []
Reflect.getPrototypeOf(obj)
- Return an object
- Gets the proto property of the prototype read object
- The corresponding Object. GetPrototypeOf (obj)
Reflect. SetPrototypeOf (obj, value} {key:)
- Set prototype (prototype)
- The second parameter is what is set on the prototype
Reflect.ownKeys (obj)
- Gets the properties of the returned object
- Only enumerable and non-enumerable are retrieved without stereotypes or attributes on the stereotype chain
Object. The keys/Reflect. OwnKeys/for difference in
- For in gets all properties of the enumerable object itself, the prototype, and the prototype chain
- Keys can only get enumerable properties of the Object itself
- Reflects. ownKeys belong only to Reflects. you can retrieve all enumerable and non-enumerable properties of the object itself, excluding stereotypes and attributes on the stereotype chain
Optimization for the in
ForEach (item => {console.log(item)}) for(let key in obj) {console.log(key)}Copy the code
Reflect.getOwnPropertyDescriptor(obj, key)
- Basic is equal to the Object. GetOwnPropertyDescriptor,
- Description object used to obtain the specified property (readable, writable, and manipulable)
Reflect.preventExtensions(obj)
- Correspond to the Object.preventExtensions method
- Es5’s objject.freeze (obj) has the same effect
- Used to make an object unextensible. It returns a Boolean value indicating whether the operation succeeded
Reflect.isExtensible (obj)
- Returns a Boolean value indicating whether the current object is extensible
++Object++
Object.is(a,b)
- Accepts two arguments for a comparison of two values
- Returns a Boolean value
- In Es6, two nans are equal
Object.keys(key)
- Traverse object
Object.getOwnPropertyNames(obj)
- Gets all properties of the object itself, including enumerable and non-enumerable properties
++Symbol++
- The seventh data type of JavaScript is a basic data type
- The word Symbol came up in a proposal for private properties of JavaScript objects. The private property proposal was not approved, but Symbol remained, which is used to set (sort of) private properties for objects
- No two symbols are equal
- Object.getOwnPropertySymbol(obj)
- Gets all Symbol attributes of itself
- usage
let obj = { a:12 } obj[Symbol('a')] = function () { } obj[Symbol('a')] = 123 console.log(obj) //{a:12, Symbol(a):123, Symbol(a):f} Copy the code
- How to get
Object.getOwnPropertySymbol(obj)
//(2)[Symbol(a),Symbol(a)]
Copy the code
- How to get the value of Symbol
By declaring variables
let a = Symbol('a'); let b = Symbol('b'); let obj = { a:12 } obj[a] = function () { } obj[b] = 123; Console. log(obj[a]) // f () {} console.log(obj[b]) // 123 console.log(obj.a) // 12 // Traverses Object.getOwnPropertySymbol(obj).forEach(item => { console.log(obj[item]) //f(){} 123 })Copy the code