ES6 引入了class(类),让JavaScript的面向对象编程变得更加简单和易于理解。
class Animal { // 构造函数,实例化的时候将会被调用,如果不指定,那么会有一个不带参数的默认构造函数. constructor(name, color) { this.name = name; this.color = color; } // toString 是原型对象上的属性 toString() { console.log(‘name:‘ + this.name + ‘,color:‘ + this.color); } } var animal = new Animal(‘dog‘, ‘white‘); //实例化Animal animal.toString(); //name:dog,color:white // console.log(animal.hasOwnProperty(‘name‘)); //true // console.log(animal.hasOwnProperty(‘toString‘)); // false // console.log(animal.__proto__.hasOwnProperty(‘toString‘)); // true class Cat extends Animal { constructor(action) { // 子类必须要在constructor中指定super 函数,否则在新建实例的时候会报错. // 如果没有置顶consructor,默认带super函数的constructor将会被添加、 super(‘cat‘, ‘white‘); this.action = action; } toString() { super.toString(); } } var cat = new Cat(‘catch‘) cat.toString(); //name:dog,color:white // 实例cat 是 Cat 和 Animal 的实例,和Es5完全一致。 // console.log(cat instanceof Cat); // true // console.log(cat instanceof Animal); // true
箭头函数与包围它的代码共享同一个this
,能帮你很好的解决this的指向问题。
ES6支持在定义函数的时候为其设置默认值
function foo(height = 50, color = ‘red‘) { console.log(height) //50 console.log(color) //red } foo();
原文:https://www.cnblogs.com/qlongbg/p/12737648.html