We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现
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 = 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._private); // 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._private); // 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; } }
原文链接:ES6 系列之私有变量的实现
The text was updated successfully, but these errors were encountered:
No branches or pull requests
ES6 系列之私有变量的实现
实现
优点
缺点
优点
缺点
优点
缺点
优点
缺点
原文链接:ES6 系列之私有变量的实现
The text was updated successfully, but these errors were encountered: