ES6 Precautions

  1. let && const

    • We have block-level scope
    • Unrepeatable statement
    • Temporary dead zone (scope statement before, unavailable, error)
    • The former is not declared, it is direct typeof error, but typeof as a defined variable does not complain output undefined (typeof no longer safe)
    • const, the statement must be assigned (not the value of the variable may not change, but the memory address of the variable points of the stored data may not change)
    • let, const, class declare global variables, property does not belong to the top-level object. ES6 from the beginning, will gradually decouple from the global variable properties of the top-level object.
    • globalThis global object (the proposal)
  2. Deconstruction

    • The string can be deconstructed assignment. const [a, b, c, d, e] = 'hello';
    • When destructuring assignment, if the right side of the equal sign is a numerical and Boolean values, it will first turn objects
    	let {toString: s} = 123;
    	s === Number.prototype.toString
    
  3. String Methods

    • includes(), startsWith(), endsWith() ,padStart(),padEnd(), padStart(),padEnd()
  4. digital

    • Number.isFinite (), Number.isNaN () and isNaN () distinction, isNaN () will be converted to digital, such as isNaN ( 'a') // true
    • Number.isInteger(), Number.isSafeInteger()
    • The method used to determine the number of Math.sign in the end is a positive, negative, or zero
    • The exponential operator (**)
  5. function

    • length property, meaning that the expected number of parameters passed to the function, and specifies the default value, this parameter is not statistics
    • rest parameters (in the form of a variable name ...), you can not have other parameters after the rest parameters
    • attribute name, function name var f = function () {}; // es5 f.name => "" // es6 f.name => "f"
    • this object within the arrow function is defined when the object is located
    • Arrow can not function as a constructor
    • Arrow can not function arguments object
    • Generator function can not be used as a function of the arrow
  6. Array

    • Extended operator, copy the array, merge array
    • And destructuring assignment binding const [first, ... rest] = [1, 2, 3, 4, 5];
    • The string to a real array [... 'hello']
    • Iterator
    • Array.from the two types of objects into a true array: array-like objects and can traverse objects, Array.from (Set)
    • Array.of method for a set of values, into an array Array.of (3, 11, 8)
    • find () and findIndex ()
    • fill() // let a = []; a.length =100; a.fill(1) // [1,1,1,1,…100个1]
    • includes()
    • flat () / flatMap () flattened array
    • Vacancy array, ES6 is clear the space into undefined // Array (3) // [,,,]
  7. Objects

    • Object.getOwnPropertyNames contains all the properties of the object itself (excluding Symbol properties, including but not enumerated attribute)
    • Object.getOwnPropertySymbols returns an array containing all the properties of the object itself Symbol keys
    • Reflect.ownKeys returns an array containing all the key names of the object itself, regardless of the key name is Symbol or string, whether or not enumerable
    • Extended operator // {... {b: 2}, a: 1} // {b: 2, a: 1}
    • Object.is like ===
    • Object.assign (target, source1, source2) the source object (source) of all enumerated attribute, copied to the target object (target) ( shallow copy )
    • Object.getPrototypeOf() => proto
    • Object.keys (), Object.values ​​(), Object.entries () (an array of key-value pairs)
  8. set / map

    • Inside Set, two are equal NaN
    • Map traversal sequence is inserted in order
  9. promise

    //	加载图片
    const preloadImage = function (path) {
      return new Promise(function (resolve, reject) {
        const image = new Image();
        image.onload  = resolve;
        image.onerror = reject;
        image.src = path;
      });
    };
    
    • Promise.all / Promise.race
  10. Iterator

    • Any data structure as long as the deployment Iterator interface, to complete traversal for ... of
    • Object (Object) is no reason why the default deployment Iterator interface, which is because the properties of the object first traversal, after which traverse the property is uncertain
    • Get Iterator
    let iter = [1,2,3][Symbol.iterator]()
    iter.next()  //{value: 1, done: false}
    iter.next()  //{value: 2, done: false}
    iter.next()  //{value: 3, done: true}
    
  • Iterator interface = call occasions> destructuring assignment, expansion operators, yield *, for ... of, Array.from (), Map (), Set (), Promise.all / Promise.race
  1. async
    • async function can be seen as a plurality of asynchronous operations, packed into a Promise object, and then await the command syntax of the command is the internal sugar
    • Internal async function return value returned by the statement, will be the method parameters then the callback function
    • No return, returned resolve (undefined)
    • Internal async function throws an error will result Promise returned object becomes reject state.
    			async function f() {
    			  throw new Error('出错了');
    			}
    			
    			f().then(
    			  v => console.log(v),
    			).catch(e=>{
    				console.log(e)
    			})
    
    1. await command can only appear within the async function, otherwise it will error

Guess you like

Origin blog.csdn.net/zdhanunity/article/details/95303100