【发布时间】:2018-10-01 16:54:26
【问题描述】:
在下面的测试代码中,工厂函数创建了一个对象。新对象内部有 2 个方法,totalCost 和 withShipping。有没有我可以使用的模式允许withShipping 使用totalCost 的返回值?按照配置,它会引发错误。非常感谢您的帮助!
"use strict"
function factoryTest(x) {
let returnTest = {
numberOfEngines: x.numberOfEngines,
costPerEngine: x.costPerEngine,
totalCost: function() {
return x.numberOfEngines * x.costPerEngine;
},
withShipping: function() {
return x.totalCost() * 2;
}
}
return returnTest;
}
let aircraft = factoryTest({numberOfEngines: 2, costPerEngine: 40000});
console.log(aircraft.totalCost());
console.log(aircraft.withShipping());
【问题讨论】:
标签: javascript methods javascript-objects