在阅读 《ECMAScript 6 入门》的时候,零散的看到有私有变量的实现,因此在此总结一篇。html
class Example { constructor() { this._private = 'private'; } getName() { return this._private } } var ex = new Example(); console.log(ex.getName()); // private console.log(ex._private); // private
/** * 实现一 */ class Example { constructor() { var _private = ''; _private = 'private'; this.getName = function() {return _private} } } var ex = new Example(); console.log(ex.getName()); // private console.log(ex._private); // undefined
/** * 实现二 */ const Example = (function() { var _private = ''; class Example { constructor() { _private = 'private'; } getName() { return _private; } } return Example; })(); var ex = new Example(); console.log(ex.getName()); // private console.log(ex._private); // undefined
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()); // private console.log(ex.name); // undefined
/** * 实现一 */ const _private = new WeakMap(); class Example { constructor() { _private.set(this, 'private'); } getName() { return _private.get(this); } } var ex = new Example(); console.log(ex.getName()); // private console.log(ex.name); // undefined
若是这样写,你可能以为封装性不够,你也能够这样写:git
/** * 实现二 */ 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()); // private console.log(ex.name); // undefined
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 字段呢?好比说这样:es6
class Foo { private value; equals(foo) { return this.value === foo.value; } }
简单点来讲,就是嫌麻烦,固然也有性能上的考虑……github
举个例子,若是咱们不使用 #,而是使用 private 关键字:编程
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 做为参数传入了 foo1 的实例方法中。闭包
那么咱们能够获取 foo2.value 的值吗?若是咱们直接 foo2.value
确定是获取不到值的,毕竟是私有变量,但是 equals 是 Foo 的一个类方法,那么能够获取到的吗?异步
答案是能够的。编程语言
其实这点在其余语言,好比说 Java 和 C++ 中也是同样的,类的成员函数中能够访问同类型实例的私有变量,这是由于私有是为了实现“对外”的信息隐藏,在类本身内部,没有必要禁止私有变量的访问,你也能够理解为私有变量的限制是以类为单位,而不是以对象为单位,此外这样作也能够为使用者带来便利。函数
既然获取值是能够的,那么打印的结果应该为 true,可是若是咱们传入的值不是 Foo 的实例,而是一个其余对象呢?性能
var foo1 = new Foo(); console.log(foo1.equals({ value: 2 }));
固然这里代码也是能够正常运行的,可是对于编译器来讲,就有一点麻烦了,由于编译器不知道 value 究竟是 foo 的正常属性仍是私有属性,因此编译器须要作判断,先判断 foo 是否是 Foo 的实例,而后再接着获取值。
这也意味着每次属性访问都须要作这样一个判断,而引擎已经围绕属性访问作了高度优化,懒得改,并且还下降速度。
不过除了这个工做以外,还会有一些其余的内容须要考虑,好比说:
关于使用 # 而不使用 private 更多的讨论能够参考这个 Issue。
固然这些问题均可以被解决啦,就是麻烦了点。
而若是你选择 #,实现的方式将跟 JavaScript 对象属性彻底没有关系,将会使用 private slots
的方式以及使用一个新的 slot 查找语法,总之就是会比 private 的实现方式简单不少。
ES6 系列目录地址:https://github.com/mqyqingfeng/Blog
ES6 系列预计写二十篇左右,旨在加深 ES6 部分知识点的理解,重点讲解块级做用域、标签模板、箭头函数、Symbol、Set、Map 以及 Promise 的模拟实现、模块加载方案、异步处理等内容。
若是有错误或者不严谨的地方,请务必给予指正,十分感谢。若是喜欢或者有所启发,欢迎 star,对做者也是一种鼓励。