使用(函数(窗口,文档,未定义){...})(窗口,文档)赋予什么优势? [英] What advantages does using (function(window, document, undefined) { ... })(window, document) confer?

查看:76
本文介绍了使用(函数(窗口,文档,未定义){...})(窗口,文档)赋予什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这种模式是新的热点,但我不明白它的优点是什么,我不明白范围的含义。

I guess using this pattern is the new hotness, but I don't understand what the advantage is and I don't understand the scoping implications.

模式:

(function(window, document, undefined){
  window.MyObject = {
    methodA: function() { ... },
    methodB: function() { ... }
  };
})(window, document)

所以我对此有几个问题。

So I have several questions about this.

封装一个是否有特别的优势像这样的对象?

为什么窗口文档被输入而不是正常访问?

为什么要哎呀是未定义被传入?

将我们正在创建的对象直接附加到窗口这是一个特别好的主意吗?

Is there a particular advantage to encapsulating an object like this?
Why are window and document being fed in instead of just being accessed normally?
Why the heck is undefined being passed in?
Is attaching the object we're creating directly to window a particularly good idea?

我已经习惯了我称之为Crockford风格的Javascript封装(因为我把它从Douglas Crockford Javascript视频中删除了)。

I'm used to what I'll call the Crockford style of Javascript encapsulation (because I got it off the Douglas Crockford Javascript videos).

NameSpace.MyObject = function() {
  // Private methods
  // These methods are available in the closure
  // but are not exposed outside the object we'll be returning.
  var methodA = function() { ... };

  // Public methods
  // We return an object that uses our private functions,
  // but only exposes the interface we want to be available.
  return {

    methodB: function() {
      var a = methodA();
    },
    methodC: function() { ... }

  }
// Note that we're executing the function here.
}();

其中一种模式在功能上优于另一种吗?第一个是另一个的演变吗?

Is one of these patterns functionally better than the other? Is the first one an evolution of the other?

推荐答案


为什么窗口和文档被输入而不仅仅是正常访问?

Why are window and document being fed in instead of just being accessed normally?

一般来说,为了加强标识符解析过程,将它们作为局部变量可以提供帮助(虽然IMO的性能改进可以忽略不计。)

Generally to fasten the identifier resolution process, having them as local variables can help (although IMO the performance improvements may be negligible).

在非浏览器环境中传递全局对象也是一种广泛使用的技术,你没有 window 全局范围内的标识符,例如:

Passing the global object is also a widely used technique on non-browser environments, where you don't have a window identifier at the global scope, e.g.:

(function (global) {
  //..
})(this); // this on the global execution context is 
          // the global object itself




为什么heck未定义传入?

Why the heck is undefined being passed in?

这是因为 undefined ECMAScript 3中的全局属性是可变的,这意味着有人可以更改其影响代码的值,例如:

This is made because the undefined global property in ECMAScript 3, is mutable, meaning that someone could change its value affecting your code, for example:

undefined = true; // mutable
(function (undefined) {
  alert(typeof undefined); // "undefined", the local identifier
})(); // <-- no value passed, undefined by default

如果仔细观察 undefined 实际上没有被传递(函数调用上没有参数),这是获得 undefined 值的可靠方法之一,不使用属性 window.undefined

If you look carefully undefined is actually not being passed (there's no argument on the function call), that's one of the reliable ways to get the undefined value, without using the property window.undefined.

名称 undefined 在JavaScript中并不意味着什么特别的,不是像 true false 等关键字... ,它只是一个标识符。

The name undefined in JavaScript doesn't mean anything special, is not a keyword like true, false, etc..., it's just an identifier.

只是为了记录,在ECMAScript 5中,这个属性是不可写的......

Just for the record, in ECMAScript 5, this property was made non-writable...


将我们正在创建的对象直接附加到窗口是一个特别好的主意吗?

Is attaching the object we're creating directly to window a particularly good idea?

当您在另一个函数范围内时,这是用于声明全局属性的常用方法。

It's a common way used to declare global properties when you are on another function scope.

这篇关于使用(函数(窗口,文档,未定义){...})(窗口,文档)赋予什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆