var firstExample = Object.create(potentialEnergy) firstExample.height = 200 firstExample.mass = 85var secondExample = Object.create(potentialEnergy) secondExample.height = 150 secondExample.mass = 100var thirdExample = Object.create(potentialEnergy) thirdExample.height = 250 thirdExample.mass = 75// object methodvar potentialEnergy = { getPE: function () { const potential = this.mass * this.height * 9.8 return potential }}console.log(firstExample.getPE())console.log(secondExample.getPE())console.log(thirdExample.getPE())问题:在我的第一次尝试中,我使用给定的符号(var firstExample = {},以及下面的属性)创建了三个具有质量和高度属性的对象,然后(单独的代码行)我尝试将势能方法与firstExample 对象通过 --> var firstExample = Object.create(potentialEnergy) 并返回 NaN,然后,当我创建所有三个对象并且程序运行时,我执行了 Object.create(potentialEnergy),我的问题是 firstExample (和第二个/第三个)被制作成对象或只是变量,因为我目前没有使用我所教的任何一种方法(var firstExample = {} 或左括号{其中的属性}),如果它们被制作成对象,那么“Object.create”是否将potentialEnergy方法与firstExample和*将firstExample制作成一个对象?
1 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
您可以尝试更改声明的顺序:
所以,它将是:
// object method
var potentialEnergy = {
getPE: function () {
const potential = this.mass * this.height * 9.8
return potential
}
}
var firstExample = Object.create(potentialEnergy)
firstExample.height = 200
firstExample.mass = 85
var secondExample = Object.create(potentialEnergy)
secondExample.height = 150
secondExample.mass = 100
var thirdExample = Object.create(potentialEnergy)
thirdExample.height = 250
thirdExample.mass = 75
console.log(firstExample.getPE())
console.log(secondExample.getPE())
console.log(thirdExample.getPE())
添加回答
举报
0/150
提交
取消