所以我试图找出声明函数的不同方法......以及低内存使用的最佳方法是什么......
我一直在做的方式是方法#4,我猜它是ATROCIOUS。我基本上做了一堆不同的“ function XYZ(){ //stuff }
”,并一直调用它来执行操作......或多或少是一种管理所有代码并组织它的方式......不是因为性能、内存或任何技术相关的原因...... . 任何人都可以请解释一下哪种方法最好吗?(或者如果您有自己的方法,请发布)为什么?
//method 1
var sayHey = new Object();
sayHey.derp = 'derp';
sayHey.herp = function(){
alert('herp');
};
//side question: would this.derp = 'derp' be the same as sayHey.derp? if so, would it be better to use this rather than sayHey?
//method 2
var sayHey2 = function() {
return {
derp : 'derp',
herp : function(){
alert('herp');
}
}
}();
//method 3
var sayHey3 = {
derp: 'derp',
herp: function(){
alert('herp');
}
};
//method 4
var derp = 'derp';
function herp(){
alert('herp');
}