JavaScript Namespace Declaration

后端 未结 8 1922
既然无缘
既然无缘 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:46

    A succinct way to do what you're asking is create "Namespace" as an object literal like this:

    var Namespace = {
        MyClass : (function() {
            ... rest of your module
        })();
    };
    

    This could cause conflicts if you wanted to attach other details to Namespace in other files, but you could get around that by always creating Namespace first, then setting members explicitly.

提交回复
热议问题