Re: [问题] ES2015的class问题

楼主: mrbigmouth (大嘴先生)   2015-10-30 17:39:46
非常抱歉的来自打嘴巴一下
原来ES 2015的class还是可以直接对class的prototype做写入的
只是你不可以直接重设整个prototype
class A {
}
A.prototype = { //Error!
someHash: {
}
}
但是写入属性是没有问题的
class A {
}
A.prototype.a = 1;
const a = new A();
a.a //1
只是目前几乎所有ES 2015的教学文章都没看过这种作法
不知道会不会有什么问题?
此外我上篇文章提到的私有变量作法在ES 2015可以用Symbol处理
const someThing = 5;
const someThingKey = Symbol();
class A {
get someThing() {
return this[someThingKey] || someThing;
}
set someThing(value) {
this[someThingKey] = value;
}
}
const a = new A();
const b = new A();
a.someThing; //5
a.someThing = 6;
a.someThing; //6
b.someThing; //5

Links booklink

Contact Us: admin [ a t ] ucptt.com