作者mrbigmouth (大嘴先生)
看板Ajax
标题Re: [问题] ES2015的class问题
时间Fri Oct 30 17:39:46 2015
非常抱歉的来自打嘴巴一下
原来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
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 211.75.132.13
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1446197989.A.B18.html
※ 编辑: mrbigmouth (211.75.132.13), 10/30/2015 17:49:14