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