Re: [問題] ES2015的class問題
非常抱歉的來自打嘴巴一下
原來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://www.ptt.cc/bbs/Ajax/M.1446197989.A.B18.html
※ 編輯: mrbigmouth (211.75.132.13), 10/30/2015 17:49:14
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章