慕標5832272
2022-07-15 09:28:04
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
提交
取消