-1

作为 Javascript 设计模式的新手,我遇到了模块模式,但我没有得到命名空间的东西。

Addy Osmani 的关于 JS 设计模式的在线书籍的命名空间部分中,Addy 解释了这 5 种检查变量/命名空间存在的方法:

// This doesn't check for existence of "myApplication" in
// the global namespace. Bad practice as we can easily
// clobber an existing variable/namespace with the same name
var myApplication = {};

// The following options *do* check for variable/namespace existence. 
// If already defined, we use that instance, otherwise we assign a new 
// object literal to myApplication.
// 
// Option 1: var myApplication = myApplication || {};
// Option 2  if( !MyApplication ){ MyApplication = {} };
// Option 3: window.myApplication || ( window.myApplication = {} );
// Option 4: var myApplication = $.fn.myApplication = function() {};
// Option 5: var myApplication = myApplication === undefined ? {} : myApplication;

我真正不明白的是它如何解决命名问题。

假设在我的代码尝试使用 myApplication 之前设置了 myApplication。例如,使用选项 1(或实际上是所有选项),如果 myApplication 已经在使用中,似乎不会改变任何东西,但只会覆盖 myApplication 的先前值:

// Higher in some script, where I don't know about it
var myApplication = 'whatever string or object used by the script';

// A bit of code later, where I come with my little Module Pattern
var myApplication = myApplication || {}; // Using Option 1

myApplication = (function($) {
   var myAppVariable = 'blabla';

   var myAppFunction = function() {
       // Doing a few things here
   };

   return myAppFunction;
}) (jQuery);

// Using the module
myApplication.myAppFunction();

对我来说,这很令人困惑,因为我看不出它如何阻止我也踩到别人的脚趾。

4

1 回答 1

0

当您在 javascript 中加载模块时,您可能(取决于我猜的代码)将不得不覆盖模块名称空间中已经存在的任何变量。在加载之前保留模块名称的流行模式是noConflict()模式。这种模式背后的想法是,您将命名空间的原始值保存在一个变量中,如果noConflict被调用,则将命名空间替换为原始值并返回您的库。模式可以或多或少优雅地写成这样:

myModule = "some stuff ya";

(function(namespace, undefined) {
    var _module = "myModule";
    var __originalModule = namespace[_module];//holds the original value in case you wish to restore it

    /****** Code your module here following whichever module pattern you wish to follow ****/
    var module = {
        log: function() {
            if(console.log) {
                console.log(arguments);
            }
        }
    }
    /****** End of your module ****/

    //calling this method will remove your module from the namespace and replace it with whatever
    // originally held your module name.
    //This function returns your module
    module.noConflict = function() {
        namespace[_module] = __originalModule;
        return module;
    }

    namespace[_module] = module; //add your module to the namespace

})(window);

console.log(window.myModule); // => {log: function...}
var myMod = window.myModule.noConflict();
console.log(window.myModule); // => "some stuff ya"
于 2013-10-25T14:39:34.247 回答