如何命名带或不带IIFEs的JavaScript代码?

如何命名带或不带IIFEs的JavaScript代码?,javascript,namespaces,javascript-namespaces,Javascript,Namespaces,Javascript Namespaces,我一直在阅读名称空间、对象文本、IIFE等方面的内容,我试图了解以下哪种方法是命名JavaScript代码的正确方法 使用IIFE嵌套外部函数的命名空间 let myApp = myApp || {}; myApp.some_var = "someString"; myApp.some_func = (function(){ const some_const = 1; let some_other_func = function(){ console.log(some_const)

我一直在阅读名称空间、对象文本、IIFE等方面的内容,我试图了解以下哪种方法是命名JavaScript代码的正确方法

使用IIFE嵌套外部函数的命名空间

let myApp = myApp || {};

myApp.some_var = "someString";

myApp.some_func = (function(){ const some_const = 1;

let some_other_func = function(){
    console.log(some_const);
};

return {
    some_other_func: some_other_func
}

}());

myApp.another_func = (function(){ const another_const = 2;

let another_func = function(){
    myApp.some_func.some_other_func();
};

return {
    another_func: another_func
}

}());
let myApp = myApp || {};

myApp.some_var = "someString";

myApp.some_func = function(){ const some_const = 1;

let some_other_func = function(){
    console.log(some_const);
};

return {
    some_other_func: some_other_func
}

};

myApp.another_func = function(){ const another_const = 2;

let another_func = function(){
    myApp.some_func.some_other_func();
};

return {
    another_func: another_func
}

};
带有嵌套外部函数的命名空间,不使用IIFE

let myApp = myApp || {};

myApp.some_var = "someString";

myApp.some_func = (function(){ const some_const = 1;

let some_other_func = function(){
    console.log(some_const);
};

return {
    some_other_func: some_other_func
}

}());

myApp.another_func = (function(){ const another_const = 2;

let another_func = function(){
    myApp.some_func.some_other_func();
};

return {
    another_func: another_func
}

}());
let myApp = myApp || {};

myApp.some_var = "someString";

myApp.some_func = function(){ const some_const = 1;

let some_other_func = function(){
    console.log(some_const);
};

return {
    some_other_func: some_other_func
}

};

myApp.another_func = function(){ const another_const = 2;

let another_func = function(){
    myApp.some_func.some_other_func();
};

return {
    another_func: another_func
}

};
具有内部嵌套函数的命名空间

let myApp = (function() { let some_var = "someString";

let some_func = function(){
    const some_const = 1;

    let some_other_func = function(){
        console.log(some_const);
    };

    return {
        some_other_func: some_other_func
    }
};

let another_func = function(){
    const another_const = 2;

    let another_func = function(){
        some_func.some_other_func();
    };

    return {
        another_func: another_func
    }
};

return {
    some_var: some_var,
    some_func: some_func,
    another_func: another_func
}

}());
let a_func = (function(){ let some_var = "someString"; }());

let some_func = (function(){ const some_const = 1;

let some_other_func = function(){
    console.log(some_const);
};

return {
    some_other_func: some_other_func
}

}(another_func, a_func));

let another_func = (function(){ const another_const = 2;

let another_func = function(){
    some_func.some_other_func();
};

return {
    another_func: another_func
}

}(a_func, some_func));
生命功能

let myApp = (function() { let some_var = "someString";

let some_func = function(){
    const some_const = 1;

    let some_other_func = function(){
        console.log(some_const);
    };

    return {
        some_other_func: some_other_func
    }
};

let another_func = function(){
    const another_const = 2;

    let another_func = function(){
        some_func.some_other_func();
    };

    return {
        another_func: another_func
    }
};

return {
    some_var: some_var,
    some_func: some_func,
    another_func: another_func
}

}());
let a_func = (function(){ let some_var = "someString"; }());

let some_func = (function(){ const some_const = 1;

let some_other_func = function(){
    console.log(some_const);
};

return {
    some_other_func: some_other_func
}

}(another_func, a_func));

let another_func = (function(){ const another_const = 2;

let another_func = function(){
    some_func.some_other_func();
};

return {
    another_func: another_func
}

}(a_func, some_func));

编辑:在我自己的特定示例中,代码将在node.js中运行,“应用程序”将少于500行代码,因此我计划将所有代码都放在一个文件中。我对那些不建议使用AMD、CommonJS、Browserify、Webpack、ES6模块等的答案特别感兴趣。

我认为最好的方法是使用CommonJS标准,从您的代码中我可以看出您已经在使用ECMAScript 6,所以最好的方法是使用

在我自己的项目中使用-它允许我使用nodejs/CommonJS模块:

// module1.js
exports.foo = function(value) {
  return value + x;
};

exports.CONST = 1;

// module2.js
var m1 = require('module1');
m1.foo();
您提出的所有方法大体上是等效的,我个人喜欢,并且在我不能使用CommonJS时尝试使用它。我还喜欢在模块开头移动return语句,这有助于可读性:

var MyModule = (function() {
  'use strict';

  return {
    foo: foo
  };

  function foo() {
    return 1;
  } 
}());
另一个重要问题是将整个模块代码封装在IFFE中,特别是当您使用
严格模式
并连接js文件时

好吧,这可能不是你问题的答案,但也许它能帮助你看到更大的图景