javascript - JS ES6 类语法和原型混淆
问题描述
好的,据我所知,我们会像这样编写一个类构造函数
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
//anything outside of constructor belong to this.prototype
getName() {
console.log(this.name);
}
getAge() {
console.log(this.age);
}
}
所以相反,如果我写这样的东西:
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
getName() {
console.log(this.name);
}
}
User.prototype.getAge = function () {
console.log(this.age);
};
从技术上讲,它应该完全相同,对吧?还是我错了?因为当我尝试这其中的每一个时,我得到了 2 个不同的结果:
let user = new User('john', 23);
let properties = [];
for (let prop in user) {
properties.push(prop);
}
console.log(properties); //The first code result : ["name", "age"]
//The second code result : ["name", "age", "getAge"]
那么两者有什么区别呢?
解决方案
两者的区别在于 usingUser.prototype.getAge = ...
会添加带有属性描述符的属性enumerable: true
。这意味着它显示在for...in
循环中。
该
for...in
语句遍历对象的所有可枚举属性,这些属性以字符串为键(忽略以Symbol为键的),包括继承的可枚举属性。
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
getName() {
console.log(this.name);
}
}
User.prototype.getAge = function () {
console.log(this.age);
};
let user = new User('john', 23);
let properties = [];
for (let prop in user) {
properties.push(prop);
}
console.log(properties);
console.log(Object.getOwnPropertyDescriptor(User.prototype, "getName"));
console.log(Object.getOwnPropertyDescriptor(User.prototype, "getAge"));
如果您想以完全相同的方式定义原型,则必须使用相同的属性描述符。这可以通过以下方式实现Object.defineProperty()
:
Object.defineProperty(User.prototype, "getAge", {
value: function () {
console.log(this.age);
},
writable: true,
enumerable: false,
configurable: true,
});
推荐阅读
- vue.js - 那么如何使用 VueJS 在动态更改的属性上更新或重新呈现我的 html 模板?
- c# - 将异步 C# 打印/字符串重定向到 python
- javascript - PostgreSQL 日期是我查询的一天
- wcf - WCF 服务主机找不到任何服务元数据错误
- python - 如何正确地将 tkinter 指向不同文件夹中的图像?
- cassandra - Cassandra:我在日志中看到 ALTER TABLE ... 但为什么呢?是 Cassandra 内部的吗
- javascript - D3.js 在“on”事件中丢失对象引用
- android - PID:12181 java.util.ConcurrentModificationException
- javascript - 如何将数据从数组复制到顶点热图图表?
- javascript - iframe 内容的高度随着每次 iframe 调整大小以适应内容而增长