前言
在閱讀 《ECMAScript 6 入門》的時候,零散的看到有私有變量的實現(xiàn),所以在此總結(jié)一篇。
1. 約定
實現(xiàn)
class Example {  constructor() {    this._private = 'private';  }  getName() {    return this._private  }}var ex = new Example();console.log(ex.getName()); // privateconsole.log(ex._private); // private優(yōu)點
缺點
2. 閉包
實現(xiàn)一
/** * 實現(xiàn)一 */class Example { constructor() {  var _private = '';  _private = 'private';  this.getName = function() {return _private} }}var ex = new Example();console.log(ex.getName()); // privateconsole.log(ex._private); // undefined優(yōu)點
缺點
實現(xiàn)二
/** * 實現(xiàn)二 */const Example = (function() { var _private = ''; class Example {  constructor() {   _private = 'private';  }  getName() {   return _private;  } } return Example;})();var ex = new Example();console.log(ex.getName()); // privateconsole.log(ex._private); // undefined優(yōu)點
缺點
3. Symbol
實現(xiàn)
const Example = (function() {  var _private = Symbol('private');  class Example {    constructor() {     this[_private] = 'private';    }    getName() {     return this[_private];    }  }  return Example;})();var ex = new Example();console.log(ex.getName()); // privateconsole.log(ex.name); // undefined優(yōu)點
缺點
4. WeakMap
實現(xiàn)
/** * 實現(xiàn)一 */const _private = new WeakMap();class Example { constructor() {  _private.set(this, 'private'); } getName() {   return _private.get(this); }}var ex = new Example();console.log(ex.getName()); // privateconsole.log(ex.name); // undefined如果這樣寫,你可能覺得封裝性不夠,你也可以這樣寫:
/** * 實現(xiàn)二 */const Example = (function() { var _private = new WeakMap(); // 私有成員存儲容器 class Example {  constructor() {   _private.set(this, 'private');  }  getName() {    return _private.get(this);  } } return Example;})();var ex = new Example();console.log(ex.getName()); // privateconsole.log(ex.name); // undefined優(yōu)點
缺點
5. 最新提案
class Point { #x; #y; constructor(x, y) {  this.#x = x;  this.#y = y; } equals(point) {  return this.#x === point.#x && this.#y === point.#y; }}那么為什么不直接使用 private 字段呢?比如說這樣:
class Foo { private value; equals(foo) {  return this.value === foo.value; }}簡單點來說,就是嫌麻煩,當(dāng)然也有性能上的考慮……
舉個例子,如果我們不使用 #,而是使用 private 關(guān)鍵字:
class Foo { private value = '1'; equals(foo) {  return this.value === foo.value; }}var foo1 = new Foo();var foo2 = new Foo();console.log(foo1.equals(foo2));在這里我們新建了兩個實例,然后將 foo2 作為參數(shù)傳入了 foo1 的實例方法中。
	那么我們可以獲取 foo2.value 的值嗎?如果我們直接 foo2.value 肯定是獲取不到值的,畢竟是私有變量,可是 equals 是 Foo 的一個類方法,那么可以獲取到的嗎?
答案是可以的。
其實這點在其他語言,比如說 Java 和 C++ 中也是一樣的,類的成員函數(shù)中可以訪問同類型實例的私有變量,這是因為私有是為了實現(xiàn)“對外”的信息隱藏,在類自己內(nèi)部,沒有必要禁止私有變量的訪問,你也可以理解為私有變量的限制是以類為單位,而不是以對象為單位,此外這樣做也可以為使用者帶來便利。
既然獲取值是可以的,那么打印的結(jié)果應(yīng)該為 true,但是如果我們傳入的值不是 Foo 的實例,而是一個其他對象呢?
var foo1 = new Foo();console.log(foo1.equals({ value: 2}));當(dāng)然這里代碼也是可以正常運行的,但是對于編譯器來說,就有一點麻煩了,因為編譯器不知道 value 到底是 foo 的正常屬性還是私有屬性,所以編譯器需要做判斷,先判斷 foo 是不是 Foo 的實例,然后再接著獲取值。
這也意味著每次屬性訪問都需要做這樣一個判斷,而引擎已經(jīng)圍繞屬性訪問做了高度優(yōu)化,懶得改,而且還降低速度。
不過除了這個工作之外,還會有一些其他的內(nèi)容需要考慮,比如說:
關(guān)于使用 # 而不使用 private 更多的討論可以參考這個Issue。
當(dāng)然這些問題都可以被解決啦,就是麻煩了點。
而如果你選擇 #,實現(xiàn)的方式將跟 JavaScript 對象屬性完全沒有關(guān)系,將會使用 private slots 的方式以及使用一個新的 slot 查找語法,總之就是會比 private 的實現(xiàn)方式簡單很多。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點
疑難解答