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
对象属性私有化
如果没有 Symbol,可以这样实现,
利用闭包:
var Person = (function () { var _gender = ''; var P = function (name, gender) { this.name = name; _gender = gender; } P.prototype.getGender = function () { return _gender; } return P })() var p1 = new Person('小明', '男'); p1.gender // undefined p1.getGender() // '男'
使用 Symbol, 具体代码:
var Person = (function () { var s = Symbol('gender'); var P = function(name, gender) { this.name = name; this[s] = gender; } return P })() var p2 = new Person('小明', '男'); p2.gender // undefined p2[Symbol('gender')] // undefined
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Symbol 的作用
对象属性私有化
如果没有 Symbol,可以这样实现,
利用闭包:
使用 Symbol, 具体代码:
The text was updated successfully, but these errors were encountered: