将es6的class代码转换为function代码:
class User {
constructor(name) {
this.name = name
}
log() {
console.log(this.name)
}
}转换后的代码:
"use strict";
function User(name) {
if (!new.target) {
throw new TypeError(`Class constructor User cannot be invoked without 'new'`)
}
this.name = name
}
Object.defineProperty(User.prototype, "log", {
value: function () {
console.log(this.name)
},
enumerable: false,
})
const user = new User("aa")
for (const key in user) {
console.log(key)
}要点:
- use strict严格模式
- 构造函数只能通过new 关键字进行调用
- 原型上的方法不能被遍历