ES6新特性
模板字面量
模板字面量用倒引号 ( `` )做字符串的拼接
let message = `${student.name} please see ${teacher.name} in ${teacher.room} to pick up your report card.`;
解构
解构数组的值:
const point = [10, 25, -34];
const [x, y, z] = point;
console.log(x, y, z);
解构对象中的值:
const gemstone = {
type: 'quartz',
color: 'rose',
karat: 21.29
};
const {type, color, karat} = gemstone;
console.log(type, color, karat);
for…of循环
for…of 循环用于循环访问任何可迭代的数据类型。
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (const digit of digits) {
console.log(digit);
}
可以随时停止或退出 for…of 循环
for (const digit of digits) {
if (digit % 2 === 0) {
continue;
}
console.log(digit);
}
展开运算符
用三个连续的点 (…) 表示,
使你能够将字面量对象展开为多个元素
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
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);
用途2: 可变参数函数
function sum(...nums) {
let total = 0;
for(const num of nums) {
total += num;
}
return total;
}
箭头函数
ES6之前,使用普通函数把其中每个名字转换为大写形式:
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map(function(name) {
return name.toUpperCase();
});
箭头函数表示:
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map(
name => name.toUpperCase()
);
// 空参数列表需要括号
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');
如果箭头函数的主体内需要多行代码, 则需要使用常规主体语法:
- 它将函数主体放在花括号内
- 需要使用 return 语句来返回内容。
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map( name => {
name = name.toUpperCase();
return `${name} has ${name.length} characters in their name`;
});
this
// 构造函数
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();
默认参数函数
function greet(name = 'Student', greeting = 'Welcome') {
return `${greeting} ${name}!`;
}
greet(); // Welcome Student!
greet('James'); // Welcome James!
greet('Richard', 'Howdy'); // Howdy Richard!
默认值与解构
1.默认值与解构数组
function createGrid([width = 5, height = 5] = []) {
return `Generating a grid of ${width} by ${height}`;
}
createGrid(); // Generates a 5 x 5 grid
2.数组是基于位置的,我们需要传入 undefined 以跳过第一个参数(并使用默认值)来到达第二个参数
createSundae([undefined, ['Hot Fudge', 'Sprinkles', 'Caramel']]);
javascript类
class Plane {
//初始化
constructor(numEngines) {
this.numEngines = numEngines;
this.enginesActive = false;
}
//静态方法
static badWeather(planes) {
for (plane of planes) {
plane.enginesActive = false;
}
}
// //原型上的方法, 由所有实例对象共享.
startEngines() {
console.log('starting engines…');
this.enginesActive = true;
}
}