This article brings you a summary of the implementation of private variables in ES6 (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
When I read "Introduction to ECMAScript 6", I saw scattered implementations of private variables, so I will summarize it here.
class Example { constructor() { this._private = 'private'; } getName() { return this._private } } var ex = new Example(); console.log(ex.getName()); // private console.log(ex._private); // private
Simple writing method
Easy debugging
Good compatibility
External access and modification
The language does not have a matching mechanism. For example, the for in statement will enumerate all attributes
Naming conflict
/** * 实现一 */ 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
No naming conflicts
/** * 实现二 */ 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
/** * 实现二 */ 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; } }
class Foo { private value; equals(foo) { return this.value === foo.value; } }
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.value we will definitely not be able to get the value. After all, it is a private variable, but equals is a class method of Foo, so can we get it?
Member functions of a class can access private variables of instances of the same type. This is because privateness is for implementation "External" information is hidden within the class itself. There is no need to prohibit access to private variables. You can also understand that the restrictions on private variables are based on the class, not the object. In addition, this can also provide users with Bring convenience.
Since it is possible to get the value, the printed result should be true, but what if the value we pass in is not an instance of Foo, but another object?var foo1 = new Foo(); console.log(foo1.equals({ value: 2 }));
private slots method and use a new slot search syntax. In short, it will be better than The implementation of private is much simpler.
The above is the detailed content of Implementation summary of private variables in ES6 (code example). For more information, please follow other related articles on the PHP Chinese website!