ES6 语法笔记

    //如果使用let,声明的变量仅在块级作用域内有效
{
var a = 1;
let b = 2;
} console.log(a); // 1
console.log(b); // Uncaught ReferenceError: b is not defined var arr = [1,2,3] for (let i = 0, l = arr.length; i < l; ++i) {} console.log(i); //上面代码中,变量i是let声明的,当前的i只在本轮循环有效 var arr = [];
for (let i = 0, l = 10; i < l; ++i) {
arr[i] = function() {
console.log(i);
}
} arr[6](); //? //不存在变量提升 console.log(foo);
console.log(bar); //报错 var foo = 2;
let bar = 2; //块级作用域和 函数声明
function f() { console.log('I am outside!'); }
(function () {
f();
}()); function f() { console.log('I am outside!'); }
(function () { if (false) {
function f() { console.log('I am inside!'); }
} f();
}()); //函数参数的解构赋值
function add([x, y]){
return x + y;
} var result = add([1, 2]); // 3 console.log(result); // es6 的 类的概念
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
} toString() {
return '(' + this.x + ', ' + this.y + ')';
}
} var point = new Point(1,2);
console.log(point.toString()); //可以写出 立即执行的 Class 类 let person = new class {
constructor(name) {
this.name = name;
} sayName() {
return this.name;
}
}('张三'); const result = person.sayName(); // "张三" console.log(result);
上一篇:shell自动计算脚本


下一篇:(诊断)解决GitHub使用双因子身份认证“Two-Factor Athentication”后无法git push 代码的“fatal: Authentication failed for ...”错误