es5发布时间:2009-11
es6发布时间:2015-6
两个版本之间的时间跨度很大,所以ES6中的特性比较多。
下面几个是常用的:
对熟悉Java,object-c,c#等纯面向对象语言的开发者来说,都会对class有一种特殊的情怀。ES6引入了calss(类),让JavaScript的面向对象变成变得更加简单和易于理解。
class Animal {
constructor(name, color) {
this.name = name;
this.color = color;
}
toString() {
console.log(‘name:‘ + this.name + ‘,color:‘ + this.color);
}
}
var animal = new Animal(‘dog‘, ‘white‘);
animal.toString();
console.log(animal.hasOwnProperty(‘name‘));
console.log(animal.hasOwnProperty(‘toString‘));
console.log(animal.__proto__.hasOwnProperty(‘toString‘));
class Cat extends Animal{
constructor(action){
//如果没有置顶constructor,默认带super函数的constructor将会被添加
super(‘cat‘,‘white‘);
this.action = action;
}
toString(){
console.log(super.toString());
}
}
var cat = new Cat(‘catch‘)
cat.toString();
console.log(cat instanceof Cat);
cosole.log(cat instanceof Animal);
原文:https://www.cnblogs.com/meiyanstar/p/14768198.html