JavaScript Namespace Declaration

后端 未结 8 1920
既然无缘
既然无缘 2020-12-07 15:09

I created a javascript class as follow:

var MyClass = (function() {
   function myprivate(param) {
      console.log(param);
   }

   return {
      MyPublic         


        
8条回答
  •  生来不讨喜
    2020-12-07 15:44

    Checkout the namespace library, it is very lightweight and easy to implement.

    (function(){
       namespace("MyClass", MyPublic);
    
       function MyPublic(x){
         return x+1;
       }
    })();
    

    It supports automatically nesting as well

    namespace("MyClass.SubClass.LowerClass", ....)
    

    Would generate the necessary object hierarchy, if MyClass, SubClass did not already exist.

提交回复
热议问题