ES6 expansion arrays and objects

Extended es6 array

  1. Extended operator

Extended operator is a subject having the Iterator interface, the operator can use the extended

  1. Array.from()

For two types of objects into real array 1. The array-like objects (as long as there is data length attribute, can be converted into an array) can traverse 2. (Iterable) object

  1. Array.of()

A set of values ​​into an array

  1. find and findIndex

find method for finding the first qualifying array members, I did not find returns undefiend, findIndex method returns the first matching member? position, if not found returns -1,? find and are findIndex the second parameter is acceptable, this callback function to bind

  1. Examples of arrays of fill ()

fill using the given value, a filled array. The value used to initialize the array, receiving the second and third parameter to specify start and end positions

  1. Examples of the method of array entries () keys () and values ​​()

These three methods provide a new way? Used to traverse the array, returns a visitor object, you can use for..of cycle? Traversal of traversal keys are key names, values ​​is key traversal entries is traversal key-value pairs

  1. It includes an array of instances ()

It represents an array contains the given value, returns true, false otherwise

New method of the Object object

  1. Concise representation of property

es6 allow direct write functions and variables, the most object properties and methods are mainly used: 1. The function returns a value, 2. module.exports deriving 3. setter and getter properties

obj = {var 
 foo, // property 
 methods () {// Method 
 } 
}

  

  1. Traversal property

es6 methods can traverse a total of five attributes of the object

  • for..in: Loop through inheritance and object itself enumerable variable (excluding Properties Symbol)
  • Object.keys (obj): returns an array containing all enumerable object itself key attribute name
  • Object.getOwnPropertyNames (obj): returns an array containing all the object's own properties key name
  • Object.getOwnPropertySymbols (obj): Returns an array containing all the attributes of the key name itself symbol
  • Reflect.ownKeys (): returns an array containing all the key name the object itself, regardless of the key name is Symbol or string, whether or not enumerable
  1. super keyword

js in this keyword always refers to the current object function resides, es6 added another similar keyword super, pointing to the current object's prototype object, super can only be used in a method object, and the method is shorthand property

{proto = const 
   foo: 'Hello' 
  } 
  const {obj = 
   foo: 'World', 
   Find () { 
    return properties of the prototype super.foo // foo access attribute of obj 
   } 
  } 
 Object.setPrototypeOf (obj, proto) // obj set of prototype proto 
 obj.find () // 'Hello'

  

  1. New methods of objects

With the value equal to the algorithm used to compare two values ​​are exactly equal Object.is('foo', 'foo') // true Object.is({}, {}) // false

  • Object.assign(target, source)

The combined object, the original object of all the enumerable property, copied to the target object

  • Common uses
    • Add properties to an object
    Point {class 
       constructor (x, y) { 
        Object.assign (the this, {x, y}) // add attributes x and y on an object instance of class Point 
       } 
      }
    

      

    • Add method as an object
    Object.assign (SomeClass.prototype, {// add the object prototype method 
      the someMethod (arg1, arg2) {} 
      antherMethod () {} 
     })
    

      

    • Object cloning
    function clone(origin) {
       return Object.assign({}, origin)
      }
      function clone(origin) {
         let originProto = Object.getPrototypeOf(origin);
         return Object.assign(Object.create(originProto), origin);
       }
    

      

    • Merge multiple objects const merge = (target, ...sources) => Object.assign(target, ...sources)
  1. __proto__属性,Object.setPrototypeOf() Object.getPrototypeOf()

js language object is achieved by a prototype inheritance chain, ES6 prototype provides additional methods __proto__ chain properties, Object.getPrototypeOf () Gets the prototype prototype property Object.setPrototypeOf (obj, proto), Object.create () Prototype provided Attributes

  1. Object.keys() Object.Values() Object.entries()

Returns an array of object properties

Guess you like

Origin www.cnblogs.com/kuishen/p/11113189.html