亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建對象(它有效,但這只是一個變量還是一個對象) - js

創建對象(它有效,但這只是一個變量還是一個對象) - js

慕標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())


查看完整回答
反對 回復 2022-07-15
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號