Javascript - 在对象中存储函数 - 不好的做法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8902687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - Storing function in object - bad practice?
提问by Zar
Is it considered bad coding-practice to store functions in an object instead of just defining them (and therefore globally)?
将函数存储在对象中而不是仅仅定义它们(因此是全局的)是否被认为是糟糕的编码实践?
Consider:
考虑:
1.
1.
Foo = {
bar: function() {
alert("baz");
}
}
Foo.bar();
Foo.bar();
vs.
对比
2.
2.
function bar() {
alert("baz");
}
bar();
bar();
Sure, it might be slightly less code for the second example, but when you start to get lots of functions - it will get messy.
I find it way, way, cleaner to, for example, use Game.update()
instead of using updateGame(); or similar. When getting deeper, like Game.notify.admin(id)
and so on, it gives you even prettier code.
当然,第二个示例的代码可能会稍微少一些,但是当您开始获得大量函数时 - 它会变得混乱。我发现它的方式,方式,更干净,例如,使用Game.update()
而不是使用 updateGame(); 或类似。当越来越深入时,比如Game.notify.admin(id)
等等,它会给你更漂亮的代码。
Is there any downsides by storing a function in an object?
将函数存储在对象中有什么缺点吗?
回答by Darin Dimitrov
The first approach is preferred. This way you are explicitly defining the scope of your functions instead of polluting the global scope. There are no downsides of using the first approach. Only upsides :-)
第一种方法是首选。通过这种方式,您可以明确定义函数的范围,而不是污染全局范围。使用第一种方法没有缺点。只有好处:-)
Conclusion: always use the first approach to define functions. The second is like javascript in the 90s, let's leave it rest in peace back in the past and use proper scoping.
结论:始终使用第一种方法来定义函数。第二个就像 90 年代的 javascript,让我们让它安息在过去,并使用适当的范围。
回答by Karl Mendes
In this specific case go with the first one. But if you Foo object gets really complex you might want to use another approach that will give you the opportunity to use a constructor. And also the first approach sometimes is not the best when it comes to the function's scope:
在这种特定情况下,请选择第一个。但是如果你的 Foo 对象变得非常复杂,你可能想要使用另一种方法来让你有机会使用构造函数。而且当涉及到函数的作用域时,第一种方法有时也不是最好的:
function Foo(appName){
this.name = appName;
}
Foo.prototype.Bar = function(){
alert(this.name)
}
var a = new Foo("baz");
a.Bar();
回答by RobG
There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:
命名空间对象没有魔法,如果您使用大量全局变量,也不一定会有任何问题。使用“命名空间”对象的主要原因是为了减少重复全局变量名称的可能性。第二个原因是为了方便起见将相似的函数组合在一起,例如:
// Object example (suggested best practice):
// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction1;
// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction1;
Note that the above has practically the same chance of duplicates as similarly global variables:
请注意,上述内容几乎与类似的全局变量具有相同的重复机会:
// Global variable example:
myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;
Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.
当然,前者通常更受欢迎,因为它被认为更容易使用。我不是在提倡你采用第二种方法(我使用第一种),只是指出虽然创建大量全局变量存在问题,但所谓的“全局命名空间污染”作为一种危险被大大高估了。