design-patterns - javascript模块模式中 undefined variable 的意义是什么

标签 design-patterns javascript commonjs

<分区>

我遇到了以下javascript模块模式并且非常喜欢它,但是,为什么在参数中使用undefined

(function(window, document, undefined){

    'use strict';

    var MyWidget = function(){

        if (!(this instanceof MyWidget)) {
            var test = new MyWidget();
            return test.init.call(test, Array.prototype.slice.call(arguments));
        }

      var firstPrivateVar = 'aa',
          secondPrivateVar = 'bb';

      this.init = function(options){
        console.log('MyWidget.init', options);
        return true;
      };

      this.setStuff = function(){

      };

    };

    window.MyWidget = MyWidget;

})(window, document);  

最佳答案

在 ECMAScript 3 中,开发人员有权使用 undefined 关键字作为变量,即它是可变的。所以如果我要写

undefined = true;

undefined 的值将为 true,它将失去其真正的含义。 在上述场景中,我们传递了窗口对象、文档对象并且没有第三个参数。所以这意味着在 function(window, document, undefined) 中, undefined variable 实际上是未定义的,因为我们没有向它传递任何参数。

根据 ECMAScript5,这个 undefined 不再是可变的

关于design-patterns - javascript模块模式中 undefined variable 的意义是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25801805/

相关文章:

javascript - Browserify+Backbone.js "ApplicationState"共享模块

design-patterns - multiton 模式 vs 享元模式

asp.net-mvc - 我的 ASP.NET MVC 应用程序贫血

javascript - bs-tooltip 限制双向数据绑定(bind)

node.js - 当项目使用 CommonJS 模块时,create-react-app 将模块导入为 ES6 模块

javascript - 如何在 GSAP 和 Webpack 中使用 ScrollMagic

c# - 限制每个方法的参数数量?

oop - 我应该如何组织一堆单独使用的功能?

javascript - 用于捕获最后一组括号的 JS 正则表达式(不包括嵌套)

javascript - 固定水平表格标题