TypeScript class private field All In One
TypeScript class private field All In One
TypeScript class
class Person {
#name: string
constructor() {
this.#name = 'eric'
}
}
WeakMap
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _Person_name;
class Person {
constructor() {
_Person_name.set(this, void 0);
__classPrivateFieldSet(this, _Person_name, 'eric', "f");
}
}
_Person_name = new WeakMap();
ES6 class
class Person {
#name: string
constructor(name) {
this.#name = name;
}
}
const person = new Person('eric');
person;
// Person {#name: 'eric'}
person.name;
// undefined
person.#name;
// ❌ Uncaught SyntaxError: Private field '#name' must be declared in an enclosing class
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16118689.html
未经授权禁止转载,违者必究!