在线转换工具 https://babeljs.io/repl
// es6
class Person {
static name = 'lisi'
#age = null
#sex = null
constructor() {
this.name = 'zhangsan'
}
set() {
this.#age = 18
this.#sex = 'nv'
}
get() {
return this.name + '今年' + this.#age + '岁'
}
static say() {
console.log(this.name + '说我今年' + this.#age + '成年了' + this.#run())
}
#run() {
return '跑起来'
}
}
// es5转换
var _sex = /*#__PURE__*/ new WeakMap() // 私有属性映射
var _run = /*#__PURE__*/ new WeakSet() // 私有方法映射
// 创建一个自动执行的匿名函数
var Person = /*#__PURE__*/ (function () {
// 创建构造函数
function Person() {
_classCallCheck(this, Person) // 检测类型是否能将类作为函数使用, 使用new关键字创建实例
_classPrivateMethodInitSpec(this, _run) // 私有方法初始化
_defineProperty(this, 'name', 'lisi') // 公共属性初始化赋值
// 私有属性初始化赋值
_classPrivateFieldInitSpec(this, _sex, {
writable: true,
value: null
})
this.name = 'zhangsan' // 执行class中constructor构造函数体其他语句
}
// 公共方法,静态方法初始化, 接收三个参数,实例,公共方法,静态方法
_createClass(
Person,
[
{
key: 'set',
value: function set() {
this.name = 'wangwu' // 公共属性赋值
this.age = 18 // 静态属性赋值
_classPrivateFieldSet(this, _sex, 'nv') // 私有属性赋值
}
},
{
key: 'get',
value: function get() {
return this.name + '今年' + this.age + '岁'
}
}
],
[
{
key: 'say',
value: function say() {
console.log(
this.name + '说我今年' + this.age + '成年了' + _classPrivateMethodGet(this, _run, _run2).call(this)
)
}
}
]
)
return Person
})()
function _run2() {
// 私有方法
return '可以跑起来了'
}
_defineProperty(Person, 'age', null) // 静态属性初始化赋值
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function')
}
}
// 类型检测
function _typeof(obj) {
// const a = (表达式1, 表达式2, ...., 表达式n)
// 从表达式1开始执行一直到n,最终返回n
return (
(_typeof =
'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator
? function (obj) {
return typeof obj
}
: function (obj) {
return obj && 'function' == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype
? 'symbol'
: typeof obj
}),
_typeof(obj) // typeof函数已被改变,并不会递归调用,调用的是前面重新赋值返回的函数
)
}
// 属性注册
function _defineProperty(obj, key, value) {
key = _toPropertyKey(key)
if (key in obj) {
// 判断是否在当前实例
Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true })
} else {
obj[key] = value
}
return obj
}
// 对象key类型检测
function _toPropertyKey(arg) {
var key = _toPrimitive(arg, 'string')
return _typeof(key) === 'symbol' ? key : String(key)
}
function _toPrimitive(input, hint) {
if (_typeof(input) !== 'object' || input === null) return input
var prim = input[Symbol.toPrimitive]
if (prim !== undefined) {
var res = prim.call(input, hint || 'default')
if (_typeof(res) !== 'object') return res
throw new TypeError('@@toPrimitive must return a primitive value.')
}
return (hint === 'string' ? String : Number)(input)
}
// 私有属性注册
function _classPrivateFieldInitSpec(obj, privateMap, value) {
_checkPrivateRedeclaration(obj, privateMap)
privateMap.set(obj, value)
}
// 设置私有属性
function _classPrivateFieldSet(receiver, privateMap, value) {
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, 'set')
_classApplyDescriptorSet(receiver, descriptor, value)
return value
}
// 判断映射表中是否存在这个映射,存在即返回,否则抛错处理
function _classExtractFieldDescriptor(receiver, privateMap, action) {
if (!privateMap.has(receiver)) {
throw new TypeError('attempted to ' + action + ' private field on non-instance')
}
return privateMap.get(receiver)
}
// 将最新的值更新到映射表中
function _classApplyDescriptorSet(receiver, descriptor, value) {
if (descriptor.set) {
descriptor.set.call(receiver, value)
} else {
if (!descriptor.writable) {
throw new TypeError('attempted to set read only private field')
}
descriptor.value = value
}
}
// 方法注册
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps) // 公共方法注册
if (staticProps) _defineProperties(Constructor, staticProps) // 静态方法注册
Object.defineProperty(Constructor, 'prototype', { writable: false })
return Constructor
}
// 对公共、静态方法遍历注册
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable = descriptor.enumerable || false
descriptor.configurable = true
if ('value' in descriptor) descriptor.writable = true
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
}
}
// 私有方法注册
function _classPrivateMethodInitSpec(obj, privateSet) {
_checkPrivateRedeclaration(obj, privateSet)
privateSet.add(obj)
}
// 是否存在私有方法,抛错处理
function _classPrivateMethodGet(receiver, privateSet, fn) {
if (!privateSet.has(receiver)) {
throw new TypeError('attempted to get private field on non-instance')
}
return fn
}
// 检测是否重复注册
function _checkPrivateRedeclaration(obj, privateCollection) {
if (privateCollection.has(obj)) {
throw new TypeError('Cannot initialize the same private elements twice on an object')
}
}
标签:function,es6,es5,obj,value,descriptor,key,return,class
From: https://www.cnblogs.com/JunLan/p/17215881.html