//deom1 class Super{ sayName(){ //do some thing here } } //通过Super.prototype可以访问到sayName方法,这种形式定义的方法,都是定义在prototype上 var a = new Super() var b = new Super() a.sayName === b.sayName //true //所有实例化之后的对象共享prototypy上的sayName方法
//demo2 class Super{ sayName =()=>{ //do some thing here } } //通过Super.prototype访问不到sayName方法,该方法没有定义在prototype上 var a = new Super() var b = new Super() a.sayName === b.sayName //false //实例化之后的对象各自拥有自己的sayName方法,比demo1需要更多的内存空间
多重箭头函数就是一个高阶函数,相当于内嵌函数
1 2 3 4 5 6 7
const add = x => y => y + x; //相当于 function add(x){ return function(y){ return y + x; }; }
箭头函数常见错误
1 2 3 4 5 6 7 8
let a = { foo: 1, bar: () => console.log(this.foo) }