下面的代碼只列印事件內容,我怎樣才能使其列印WheatherData.value數字?我是從java來的,我習慣于這樣做。
。
const Event = (time, place)=> {
var _time = time
var _place = place
return {
getTime() {
return _time
},
getPlace() {
return _place
},
updateTime() {
_time = new Date(Date.now()
},
toString() {
return 'Time: '/span> _time '
地點: ' _place
}
}
}
const WeatherData = (value, time, place) => {
var _event = Event(time, place)
var _value = value
const getValue = (/span>) => { return _value }
const toString = () => { return '
值:' _value _event.toString() }
return Object.assign({ getValue, toString }, _event)
}
const wd = WeatherData(10. 1, new Date(Date. now(), 'Chisinau, Moldova')
console.log(wd.toString))
/Time: Sat Sep 18 2021 08:49:10 GMT 0200 (Central European Summer Time)
//Place: 摩爾多瓦,基希訥烏。
//No value printed
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
return Object.assign({ getValue, toString }, _event)
上面那行造成了問題。當你把_event物件分配給{ getValue, toString }物件時,你只是覆寫了WeatherData函式的toString方法。相反,只需從你的WeatherData函式中回傳{ getValue, toString },如下所示;
const WeatherData = (value, time, place)=> {
var _event = Event(時間,地點)。
var _value = value
const getValue = (/span>) => { return _value }
const toString = () => { return '
值:'/span> _value '
' _event.toString() }
return { getValue, toString }
}
uj5u.com熱心網友回復:
return Object.assign({}, _event, { getValue, setValue, toString })/code>
這對我來說是有效的,是正確的方法嗎?我這樣覆寫了event的toString方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/329127.html
標籤:
