[英]More than one javascript namespace?
I was wondering whether there is any associated performance problems with using only one namespace or whether assigning more than one is better ? 我想知道仅使用一个名称空间是否存在任何相关的性能问题,或者分配多个名称空间是否更好?
By this I mean 我的意思是
(function (myApp, $, undefined) {
} (window.myApp = window.myApp || {}, jQuery));
And assign every function / object / property to the myApp
namespace or whether it is more efficent from a performance perspective to have myApp
for common functions etc and then have 并将每个功能/对象/属性分配给
myApp
命名空间,或者从性能的角度来看,让myApp
用于通用功能等是否更有效
(function (myAppSettings, $, undefined) {
} (window.myAppSettings = window.myAppSettings || {}, jQuery));
And so forth ? 等等?
Edit : to clarify the generic settings
to myAppSettings
per @pimvdb very correct comment 编辑 :为每个@pimvdb澄清
myAppSettings
的通用settings
,非常正确的注释
Generally a shallow tree is better than a deep tree (citation from here ). 通常,浅树胜于深树( 此处引用)。 So, depending on the circumstances, it may be better to use more than one namespace.
因此,根据不同的情况,它可能是更好地使用多个命名空间。 The cited article claims a single namespace is negligible in terms of perfomance though.
引用的文章声称,就性能而言,单个名称空间可以忽略不计。
I don't think there is any difference from a performance point of view. 从性能的角度来看,我认为没有任何区别。 Getting an object property is O(1) in today's implementations.
在今天的实现中,获取对象属性是O(1) 。
But a name like settings
is so generic you'd better not make it collide with another library's settings
. 但是,诸如
settings
类的名称是如此通用,您最好不要使其与另一个库的settings
冲突。 Since these settings are part of myApp
, it makes most sense (and it is most safest) to put it in the myApp
object. 由于这些设置是
myApp
一部分,因此将其放入myApp
对象是最有意义的(也是最安全的)。
If every library would be setting window.settings
, that would be a big issue. 如果每个库都将设置
window.settings
,那将是一个大问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.