Mar*_*c W 18
在Javascript中没有像C++中那样的官方命名空间概念.但是,您可以将函数包装在Javascript对象中以模拟名称空间.例如,如果要在调用的"命名空间"中编写函数MyNamespace,则可以执行以下操作:
var MyNamespace = {};
MyNamespace.myFunction = function(arg1, arg2) {
// do things here
};
MyNamespace.myOtherFunction = function() {
// do other things here
};
Run Code Online (Sandbox Code Playgroud)
然后,要调用这些函数,你会写MyNamespace.myFunction(somearg, someotherarg);和MyNamespace.myOtherFunction();.
我还应该提到,在Javascript中有许多不同的方法来执行命名空间和类类事物.我的方法只是其中之一.
有关更多讨论,您可能还想查看此问题.