ES6 learning grammar ...

I. Scope (const var let)

Core: Minimize the use of var, using a constant const, let block-level scopes

5 button, click on the i button to achieve display i button is clicked.

  var btn = document.getElementsByTagName("button");
  // ES5 use closures to solve the problem var scope
  for (var i = 0; i < btn.length; i++) {
    (function(name){
      btn[i].addEventListener("click",function(){
      the console.log ( "This is the first" +  (name + . 1 + "button" )});       
    })(i);    
  }
  // ES6 using block-level scope to address the scope of the problem
  for (let i = 0; i < btn.length; i++) {    
      btn[i].addEventListener("click",function(){
      console.log("ES6这是第" + (i+1+ "个按钮")});     
    }
 
二、ES6语法- ``(tab键上键)
"" ''表示字符串,ES6中``也可表示字符串,且可以实现换行功能
如:
let n = 'a'
+'b';
输出ab
let m = `ab
`;
输出ab

Guess you like

Origin www.cnblogs.com/xiaoguniang0204/p/12263478.html