我有兩個內部函數的對象。第一個工作完美,但第二個返回錯誤比「helper.calc.add不是一個函數」。怎麼了?JS。對象函數返回函數返回
例如第一:
var calc = (function() {
var add;
var remove;
// some functions add and remove...
return {
add: add,
remove: remove
}
})();
calc.add(1);
其次:
var helper = (function() {
return {
calc: function() {
var add;
var remove;
// some functions add and remove...
return {
add: add,
remove: remove
}
}
}
})();
helper.calc.add(1);
在控制檯:
1
Uncaught TypeError: helper.calc.add is not a function
小提琴:https://jsfiddle.net/pk4tsnnt/
'helper.calc'是'function',不'object' ...嘗試'helper.calc() .add(1);' – Rayon
您可能打算立即調用函數'calc'。 – RemcoGerlich