如何避免Javascript中的长名称空间
我们正在启动一个新项目,并决定使用javascript名称空间来组织客户端代码。问题是,我们已经注意到,我们可以很容易地以超长名称空间结束,这些名称空间很难记住 myProject.components.myComponent.doSomethig 有没有更好的方法来实现这一点或以某种方式创建某种别名如何避免Javascript中的长名称空间,javascript,namespaces,Javascript,Namespaces,我们正在启动一个新项目,并决定使用javascript名称空间来组织客户端代码。问题是,我们已经注意到,我们可以很容易地以超长名称空间结束,这些名称空间很难记住 myProject.components.myComponent.doSomethig 有没有更好的方法来实现这一点或以某种方式创建某种别名 谢谢。您始终可以在局部变量中存储子名称空间: var myComponent = myProject.components.myComponent; 始终可以在局部变量中存储子名称空间: var
谢谢。您始终可以在局部变量中存储子名称空间:
var myComponent = myProject.components.myComponent;
始终可以在局部变量中存储子名称空间:
var myComponent = myProject.components.myComponent;
如果您在模块代码周围使用作用域函数,如果您不使用,那么您可以很容易地创建本地别名,作为给定模块中所有函数共享的变量 例如,定义myComponent组件的代码:
(function() {
var comp = {};
// Export our component
myProject.components.myComponent = comp;
// add things to `comp`; since `comp` and `myProject.components.myComponent`
// refer to the same object, adding to `comp` is adding to the one object
// they both refer to and so you can access those things via either reference
})();
类似地,如果您正在编写使用多个组件的应用程序代码:
(function() {
var thisComp = myProject.components.thisComponent,
thatComp = myProject.components.thatComponent;
// Use `thisComp` and `thatComp`
})();
如果您在模块代码周围使用作用域函数,如果您不使用,那么您可以很容易地创建本地别名,作为给定模块中所有函数共享的变量 例如,定义myComponent组件的代码:
(function() {
var comp = {};
// Export our component
myProject.components.myComponent = comp;
// add things to `comp`; since `comp` and `myProject.components.myComponent`
// refer to the same object, adding to `comp` is adding to the one object
// they both refer to and so you can access those things via either reference
})();
类似地,如果您正在编写使用多个组件的应用程序代码:
(function() {
var thisComp = myProject.components.thisComponent,
thatComp = myProject.components.thatComponent;
// Use `thisComp` and `thatComp`
})();
在JavaScript中,可以对长名称空间进行快捷引用
var myComp = myProject.components.myComponent;
myComp.func1();
这仅供参考。你可以用其他长名字来写,这样写的话就少了
var getEl = document.getElementById,
myEl = getEl('divId');
您还可以使用组织代码来组织代码
//文件:myProject/components/myComponent.js
define(function(){
var myComponent ={};
myComponent.func1 = function(){
...
}
return myComponent;
});
//文件:myProject/main.js
require(['myProject/components/myComponent', 'myProject/components/myComponent2'],
function(comp1, comp2){
var main = {};
main.init = function() {
...
comp1.func1();
}
});
//文件:myProject/index.html
<script src="libs/require.js" data-main="myProject/main"></script>
在JavaScript中,可以对长名称空间进行快捷引用
var myComp = myProject.components.myComponent;
myComp.func1();
这仅供参考。你可以用其他长名字来写,这样写的话就少了
var getEl = document.getElementById,
myEl = getEl('divId');
您还可以使用组织代码来组织代码
//文件:myProject/components/myComponent.js
define(function(){
var myComponent ={};
myComponent.func1 = function(){
...
}
return myComponent;
});
//文件:myProject/main.js
require(['myProject/components/myComponent', 'myProject/components/myComponent2'],
function(comp1, comp2){
var main = {};
main.init = function() {
...
comp1.func1();
}
});
//文件:myProject/index.html
<script src="libs/require.js" data-main="myProject/main"></script>
这可能是最普遍的解决方案,只是评论一下为什么应该是有意义的。如果窗口对象被重新定义,作用域函数可以防止命名空间冲突,这可能会在以后的代码中导致灾难。这可能是最通用的解决方案,只是为了说明为什么应该是有意义的。作用域函数可防止命名空间冲突,如果重新定义窗口对象,可能会在代码中导致灾难。