NodeJS最新的主要版本针对ES2015(也称为ES6)新功能特性进行了大量的补充
一
//let关键字,块级作用域
if(false){
var x = "hello";
}
console.log(x);
//const定义常量,给常量赋值报错
const numbers=[2,5,6,7,8,9,0,1];
const even = numbers.filter(x=>{
if(x%2===0){
console.log(x+' is even! ');
return true;
}
})
二
箭头函数
1、自动调用return
2、绑定自身作用域
//过去语法
function Person(name,surname,age){
this.name = name;
this.surname = surname;
this.age = age;
}
Person.prototype.getFullName =function(){
return this.name + ''+this.surname;
};
Person.older = function(person1,person2){
return (person1.age>=person2.age)?person1:person2;
}
//新语法之类语法
class Person{
constructor(name,surname,age){
this.name = name;
this.surname = surname;
this.age = age;
}
getFullName(){
return this.name+''+this.surname;
}
static older (person1,person2){
(person1.age>=person2.age)?peroson1:person2;
}
}
extend 和super 关键字来扩展Person属性的可能性
继承更为方便,声明了一个想要扩展的类,定义了一个新的构造函数,它可以使用关键字super调用父类,并重写了getFullName方法以添加对中间名的支持
class PersonWithMiddlename extends Person {
constructor (name , middlename, surname, age) {
super(name, surname, age)
this.middlename = middlename
}
getFullName() {
return this.name +''+ this.middlename + ''+this.surname
}
}
三、get 和 set
const person ={
name:'George',
surname: 'Boole',
get fullname(){
return this.name +'' +this.surname
},
set fullname(){
let parts = fullname.split('')
this.name = parts[0]
this.surname = parts[1]
}
}
console.log(person.fullname)
console.log(person.fullname= 'Alan Turing' )
console.log(person.name)