The new features in common JavaScript ES6

Reference: https://www.jianshu.com/p/ac1787f6c50f

Variable declarations: const and let

  const: constant, you must initialize the value of let: Variable

  Format: const variable A = "value" let the variable B = "value"  

  Scope: both are block-level scope

Template string:

  Example:

let a = "value 1" 

the console.log (value `:` $ {A})   // Output: Value: Value 1

Arrow function:

  Features: 1 do not need to create a function key function

     2. When only a function expression can be omitted and the return keyword {}

     3. When the function has only one parameter can be omitted ()

  Example:

// when the function relating to only one expression can be omitted} { 
the let Fn1 = (A, B) => A + B
 // arrow function 
the let Fn2 = () => {
   // function contents 
}
 // only one parameter may be omitted () 
Fn3 = A => the let {
     // function contents 
}

A function of operating parameters defaults

  Note: The default values ​​of the parameters needed to write the final surface parameters

  Example:

// text2 have default values, parameters need to write in the last 
function PrintText (text1, text2 = "123", text3 = "456" ) { 
    the console.log (text1} { `$ - $ {text2} - $ {} text3 `) 
} 

PrintText ( '0000') // output: 0000-123-456

Spread / Rest operator (...)

  Example:

// Example 1: When the iterator is used, which is a Spread operator 
function foo (X, Y, Z) { 
  the console.log (X, Y, Z); 
} 
 
the let ARR = [1,2, . 3 ]; 
foo (... ARR); // . 1 2. 3 

// example 2: when the parameters are passed to the function when the operator is a Rest: when parameters are passed to the function when the operator is a Rest : 
function foo1 (... args) { 
  the console.log (args); 
} 
foo1 ( . 1, 2,. 3,. 4,. 5);   // [. 1, 2,. 3,. 4,. 5]

It supports binary and octal literals

= oValue the let 0o10; 
the console.log (oValue); // . 8 
 
the let bValue = 0b10; // binary `0b` use 0B` or` 
the console.log (bValue); // 2

Deconstruction and an array of objects

// Object 
const Student = { 
    name: 'Sam' , 
    Age: 22 is , 
    Sex: 'M' 
} 
// Array 
// const Student = [ 'Sam', 22 is, 'M']; 

// the ES5; 
const name = student.name; 
const Age = student.age; 
const Sex = student.sex; 
the console.log (name + '---' + Age + '---' + Sex); 

// for ES6 
const {name, Age, } = Sex Student; // deconstruct 
the console.log (name + '---' + Age + '---' Sex +);

for...of 与 for ... in

  for ... of iterators for traversing a, such as an array:

let letters = ['a', 'b', 'c']
letters.size = 3for (let letter of letters) {
  console.log(letter)
}// 结果: a, b, c

  for ... in to iterate object attributes:

 let stus = ["Sam", "22", "男"]
 for (let stu in stus) {
   console.log(stus[stu])
 } // 结果: Sam, 22, 男

Not particularly aware of the following two:

   Object superclass super

   Class class syntactic sugar

 

 

A person should cultivate the trust of their habits, even in the most critical time, we must believe that their courage and perseverance. - Napoleon

Guess you like

Origin www.cnblogs.com/jingxuan-li/p/11809760.html