javascript - Object.create 中的对象属性值是常量吗?
问题描述
这是一个非常奇怪的问题,我一直在学习一些 Javascript,并且我得到了代码,但我想了解为什么会发生这种情况:
为什么我可以让 att 和 def boost 在 Object.create 之外创建但属性 hp 作为常量工作?
let Pokemon = {
def: this.def,
att: this.att,
defBoost: function() {
this.def = this.def + this.def
return this.def;
},
attBoost: function() {
this.att = this.att + this.att
return this.att;
},
hpBoost: function() {
this.hp = this.hp + this.hp
return this.hp;
}
}
let psyduck = Object.create(Pokemon, {
name: {
value: "Psyduck"
},
hp: {
value: 500
}
});
psyduck.def = 12;
psyduck.att = 20;
console.log(psyduck);
psyduck.attBoost();
psyduck.defBoost();
psyduck.hpBoost();
console.log(psyduck);
解决方案
推荐阅读
- javascript - 为什么在尝试将日期时间类型转换为日期时出现错误“未定义时刻”?
- python - 如何从当前项目向 scrapyd 添加新服务
- wso2 - WSO2 自定义中介设置信封的值
- shadow-dom - 当阴影设置为 true 时,如何仅在 stencilJS 中获取被点击的元素
- android - 应用程序在 java.net.SocketTimeoutException 上崩溃:超时(Kotlin、Retrofit)
- python - 由于 wx 问题,无法使用 view_cube 命令
- json - 无法在 swift 4 中发出发布请求
- python - 使用 datetime pandas 根据持续时间创建行
- java - 理解 Integer.highestOneBit() 方法实现背后的逻辑
- mysql - 尝试创建表时出现 MySQL 错误,