我想知道集成一个函数的方式是什么...我在下面放了4个方法。由于范围变化和/或声明的方式,是一个还是更好?也许他们中的一些或全部完全错了?任何帮助表示赞赏。非常感谢你!
//method 1
var derpHerp1 = function() {
var addMe1 = function (firstNum,secNum){
var sum = firstNum + secNum;
return sum
}
return {
derp : 'derp',
herp : function(){
alert('herp');
},
adding : addMe1(5,6)
}
}();
//method 2 (2nd line & 3rd to the last changed)
var derpHerp2 = function() {
var addMe2 = function addMe2(firstNum,secNum){
var sum = firstNum + secNum;
return sum
}
return {
derp : 'derp',
herp : function(){
alert('herp');
},
adding : addMe2(5,6)
}
}();
//method 3 (2nd line & 3rd to the last changed)
var derpHerp3 = function() {
function addMe3(firstNum,secNum){
var sum = firstNum + secNum;
return sum
}
return {
derp : 'derp',
herp : function(){
alert('herp');
},
adding : addMe3(5,6)
}
}();
//method 4
function addMe4 (firstNum,secNum){
var sum = firstNum + secNum;
return sum
}
var derpHerp4 = function() {
return {
derp : 'derp',
herp : function(){
alert('herp');
},
adding : addMe4(5,6)
}
}();
答案 0 :(得分:0)
我会这样做
function f1(a, b) {
...
}
function f2() {
...
}
obj = {
derp : 'derp',
herp : (f1),
adding : (f2)(5,6)
}