跳到主要內容

發表文章

目前顯示的是 10月, 2018的文章

ECMA TC39 將實作 javascript private的語言特性

這次ECMA TC39 技術委員在Github上通過了ECMAScript語言特性的草案 詳情可以看這個  https://github.com/tc39/proposal-class-fields class的private property將可以用 # 來表示 現在 如果class要使用private property的話,必須利用Symbol或是WeakMap特性或是其他方法來實作 var _action = Symbol ( 'action' ) ; class Person { getXXX ( ) { return this [ _action ] ( ) ; } [ _action ] ( ) { return 'hi' ; } } 未來 而現在更新之後,直接偷範例過來用 class Counter extends HTMLElement { #xValue = 0 ; get # x ( ) { return #xValue ; } set # x ( value ) { this . #xValue = value ; window . requestAnimationFrame ( this . #render . bind ( this ) ) ; } # clicked ( ) { this . #x ++ ; } constructor ( ) { super ( ) ; this . onclick = this . #clicked . bind ( this ) ; } connectedCallback ( ) { this . # render ( ) ; } # render ( ) { this . textContent = this . #x . toString ( ) ; } } window . cus