文档详情

ES6开发必知必会.pdf

发布:2017-11-20约1.34万字共61页下载文档
文本预览下载声明
ES6 必知必会 llh91100 1@ 20 16/ 1/ 13 变量和参数 let 声明 块级作用域 if(true) { let a 1 } console.log(a) // a is not defined 不可重复声明 let a 1 let a 2 // Duplicate declaration a 以前 for (var i0; i10; i++) { setTimeout(~function(a) { console.log(a) }(i), 100) } // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 现在 for (let i0; i10; i++) { setTimeout(() { console.log(i) }, 100) } // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 const 声明 定义常量 const CONSTANT a constant CONSTANT something else // CONSTANT is read-only // 重新为一个常量赋值会报错 不可只声明不赋值 const CONSTANT // Syntax error: Unexpected token 对象常量是可变的 const CONSTANT {foo: 1} CONSTANT.foo 2 // 正常运行 默认参数 以前 function work(name) { name name || Bender return name } work() // Bender 现在 function work(name Bender) { return name } work() // Bender 多个参数 function work(name Bender, hobby drinking) { return `${name} likes ${hobby}` } work() // Bender likes drinking 与普通参数混合使用 function work(foo, name Bender, hobby drinking, bar) { return `${name} likes ${hobby}` } work() // Bender likes drinking rest 参数 function foo(...args) { console.log(args); } foo(1, 2, 3) // [1, 2, 3] foo() // [] ...args 只能出现在函数参数列表的最后一个位置 // 报错 function foo(...args, name) { // Syntax error: Unexpected token console.log(name) } // 正常运行 function bar(name, ...args) { console.log(name) } bar(Bender) // Bender 告别 arguments 扩展运算符
显示全部
相似文档