Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
es6 weakMap private property
const privateProps = new WeakMap();
function _convertToLowercase(val) {
return val.toLowerCase();
}
class Person {
constructor(name) {
privateProps.set(this, {name: name});
}
getName() {
return _convertToLowercase(privateProps.get(this).name);
}
}
let p = new Person('LUSHIJIE');
console.log(p.getName())
console.log(p.name);
console.log(Object.keys(Person.prototype))
console.log(Object.getOwnPropertyNames(Person.prototype))
// let _convertToLowercase = Symbol('convertToLowercase');
// class Person {
// constructor(name) {
// this.name = name;
// }
// [_convertToLowercase](val) {
// return val.toLowerCase();
// }
// getName() {
// return this[_convertToLowercase](this.name);
// }
// }
// class Person {
// constructor(name) {
// this.name = name;
// }
// _convertToLowercase(val) {
// return val.toLowerCase();
// }
// getName() {
// return this._convertToLowercase(this.name);
// }
// }
// let p = new Person('LUSHIJIE');
// console.log(p.getName());
// p.name = 'GEXUFEI';
// console.log(p.getName())
// console.log(Object.keys(Person.prototype))
// console.log(Object.getOwnPropertyNames(Person.prototype))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment