这是我参与更文挑战的第5天,活动详情查看: 更文挑战
展开运算符
展开运算符(用三个连续的点 (…) 表示)是 ES6 中的新概念,使你能够将字面量对象展开为多个元素
const books = ["Don Quixote", "The Hobbit", "Alice in Wonderland", "Tale of Two Cities"];
console.log(...books);
Prints: Don Quixote The Hobbit Alice in Wonderland Tale of Two Cities
复制代码
展开运算符的一个用途是结合数组。
如果你需要结合多个数组,在有展开运算符之前,必须使用 Array的 concat() 方法。
const fruits = ["apples", "bananas", "pears"];
const vegetables = ["corn", "potatoes", "carrots"];
const produce = fruits.concat(vegetables);
console.log(produce);
Prints: ["apples", "bananas", "pears", "corn", "potatoes", "carrots"]
复制代码
使用展开符来结合数组
const fruits = ["apples", "bananas", "pears"];
const vegetables = ["corn", "potatoes", "carrots"];
const produce = [...fruits,...vegetables];
console.log(produce);
复制代码
剩余参数(可变参数)
使用展开运算符将数组展开为多个元素, 使用剩余参数可以将多个元素绑定到一个数组中.
剩余参数也用三个连续的点 ( … ) 表示,使你能够将不定数量的元素表示为数组.
用途1: 将变量赋数组值时:
const order = [20.17, 18.67, 1.50, "cheese", "eggs", "milk", "bread"];
const [total, subtotal, tax, ...items] = order;
console.log(total, subtotal, tax, items);
20.17 18.67 1.5 ["cheese", "eggs", "milk", "bread"]
复制代码
2: 可变参数函数
对于参数不固定的函数,ES6之前是使用参数对象(arguments)处理:
function sum() {
let total = 0;
for(const argument of arguments) {
total += argument;
}
return total;
}
复制代码
在ES6中使用剩余参数运算符则更为简洁,可读性提高:
function sum(...nums) {
let total = 0;
for(const num of nums) {
total += num;
}
return total;
}
复制代码
ES6箭头函数
ES6之前,使用普通函数把其中每个名字转换为大写形式:
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map(function(name) {
return name.toUpperCase();
});
复制代码
箭头函数表示:
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map(
name => name.toUpperCase()
);
复制代码
普通函数可以是函数声明或者函数表达式, 但是箭头函数始终都是表达式, 全程是箭头函数表达式, 因此因此仅在表达式有效时才能使用,包括:
- 变量中,
- 参数传递给函数,
- 在对象的属性中。
const greet = name => `Hello ${name}!`;
复制代码
可以如下调用:
greet('Asser');
复制代码
如果函数的参数只有一个,不需要使用()包起来,但是只有一个或者多个, 则必须需要将参数列表放在圆括号内:
// 空参数列表需要括号
const sayHi = () => console.log('Hello Udacity Student!');
// 多个参数需要括号
const orderIceCream = (flavor, cone) => console.log(`Here's your ${flavor} ice cream in a ${cone} cone.`);
orderIceCream('chocolate', 'waffle');
复制代码
一般箭头函数都只有一个表达式作为函数主题:
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map(
name => name.toUpperCase()
);
复制代码
这种函数表达式形式称为简写主体语法:
- 主体周围没有花括号,
- 自动返回表达式
但是如果箭头函数的主体内需要多行代码, 则需要使用常规主体语法:
- 它将函数主体放在花括号内
- 需要使用 return 语句来返回内容。
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map( name => {
name = name.toUpperCase();
return `${name} has ${name.length} characters in their name`;
});
复制代码
javascript标准函数this
- new 对象
const mySundae = new Sundae('Chocolate', ['Sprinkles', 'Hot Fudge']);
复制代码
sundae这个构造函数内的this的值是实例对象, 因为他使用new被调用.
2. 指定的对象
const result = obj1.printName.call(obj2);
复制代码
函数使用call/apply被调用,this的值指向指定的obj2,因为call()第一个参数明确设置this的指向
3. 上下`文对象
data.teleport();
复制代码
函数是对象的方法, this指向就是那个对象,此处this就是指向data.
4. 全局对象或 undefined
teleport();
复制代码
此处是this指向全局对象,在严格模式下,指向undefined.
箭头函数和this
对于普通函数, this的值基于函数如何被调用, 对于箭头函数,this的值基于函数周围的上下文, 换句话说,this的值和函数外面的this的值是一样的.
function IceCream() {
this.scoops = 0;
}
// 为 IceCream 添加 addScoop 方法
IceCream.prototype.addScoop = function() {
setTimeout(function() {
this.scoops++;
console.log('scoop added!');
console.log(this.scoops); // undefined+1=NaN
console.log(dessert.scoops); //0
}, 500);
};
----------
标题
const dessert = new IceCream();
dessert.addScoop();
复制代码
传递给 setTimeout() 的函数被调用时没用到 new、call() 或 apply(),也没用到上下文对象。意味着函数内的 this 的值是全局对象,不是 dessert 对象。实际上发生的情况是,创建了新的 scoops 变量(默认值为 undefined),然后递增(undefined + 1 结果为 NaN);
解决此问题的方式之一是使用闭包(closure):
// 构造函数
function IceCream() {
this.scoops = 0;
}
// 为 IceCream 添加 addScoop 方法
IceCream.prototype.addScoop = function() {
const cone = this; // 设置 `this` 给 `cone`变量
setTimeout(function() {
cone.scoops++; // 引用`cone`变量
console.log('scoop added!');
console.log(dessert.scoops);//1
}, 0.5);
};
const dessert = new IceCream();
dessert.addScoop();
复制代码
箭头函数的作用正是如此, 将setTimeOut()的函数改为剪头函数:
// 构造函数
function IceCream() {
this.scoops = 0;
}
// 为 IceCream 添加 addScoop 方法
IceCream.prototype.addScoop = function() {
setTimeout(() => { // 一个箭头函数被传递给setTimeout
this.scoops++;
console.log('scoop added!');
console.log(dessert.scoops);//1
}, 0.5);
};
const dessert = new IceCream();
dessert.addScoop();
复制代码