Re: [問題] ES2015的class問題

看板Ajax作者 (大嘴先生)時間10年前 (2015/10/30 17:39), 10年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
非常抱歉的來自打嘴巴一下 原來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
文章代碼(AID): #1MCphbiO (Ajax)
文章代碼(AID): #1MCphbiO (Ajax)