简体   繁体   English

如果在匿名函数中定义对象 - 如何使用它?

[英]If you define an object in an anonymous function — how do you use it?

I am looking at this tutorial. 我正在看这个教程。 I know that it is using an anonymous function, from this answer: Why do you need to invoke an anonymous function on the same line? 我知道它正在使用匿名函数,从这个答案: 为什么你需要在同一行上调用匿名函数?

I understand that the () at the end of the function will autoexecute the prior function -- passing (this) as the parameter. 我知道函数末尾的()将自动执行先前的函数 - 传递(this)作为参数。 I understand that the (this) in the function refers to the global this . 据我所知,函数中的(this)指的是全局的

I understand that people use this anonymous function pattern to protect against pollution in the global namespace. 我知道人们使用这种匿名函数模式来防止全局命名空间中的污染。

However, I do not understand how you would USE the cow defined in the anonymous function. 但是,我不明白你将如何使用匿名函数中定义的牛。 For instance, in python, if you imported a class cow from cow import cow , you could do Cow(name) . 例如,在python中,如果你from cow import cow ,你可以做Cow(name) Or in java you could do new Cow (name) 或者在java中你可以做new Cow (name)

If you define a cow in an anonymous function (to protect the global namespace) -- how do you use it? 如果您在匿名函数中定义一个母牛(以保护全局命名空间) - 您如何使用它?

Note the line 注意这一行

exports.Cow = Cow;

That adds a Cow property to exports , which in the calling code is this . 这会将一个Cow属性添加到exports ,在调用代码中就是this Thus, whatever this is when the anonymous function is called will have a Cow property (which is the constructor function) when the function returns. 因此,无论this当匿名函数被调用时都会有一个是Cow特性(这是构造函数)函数返回时。

EDIT: For those who want the example directly in front of them, here's the code from OP's link that is relevant to the question: 编辑:对于那些直接在他们面前想要这个例子的人来说,这里是OP链接中与该问题相关的代码:

(function(exports) {
  "use strict";

  function Cow(name) {
    this.name = name || "Anon cow";
  }
  exports.Cow = Cow;

  Cow.prototype = {
    greets: function(target) {
      if (!target)
        throw new Error("missing target");
      return this.name + " greets " + target;
    }
  };
})(this);

The result would be used in exactly the same way that one would use Cow after the following code executed: 结果的使用方式与执行以下代码后使用Cow方式完全相同:

var Cow = function (name) {
    this.name = name || "Anon cow";
}

Cow.prototype = {
    greets: function(target) {
        if (!target)
            throw new Error("missing target");
        return this.name + " greets " + target;
    }
};

The only difference being (in this case) that the anonymous function function is evaluated in strict mode regardless of the global setting, without changing the global setting. 唯一的区别是(在这种情况下)匿名函数函数在严格模式下进行评估而不考虑全局设置,而不更改全局设置。 In both cases you could do: 在这两种情况下,您都可以:

var aCow = new Cow("Buttercup");
aCow.greet("Farmer Johnson");

Output: 输出:

Buttercup greets Farmer Johnson 毛茛迎接农民约翰逊

What that code is doing is defining a Cow object, and then by the following line: 该代码正在做的是定义一个Cow对象,然后通过以下行:

exports.Cow = Cow;

The line above adds a Cow property to the window object, and the value of this property is the object constructor. 上面的行将一个Cow属性添加到window对象,该属性的值是对象构造函数。

At this point, because window is global in a browser, you can create new Cow objects by the following in another file: 此时,因为window在浏览器中是全局的,所以可以通过以下文件在另一个文件中创建新的Cow对象:

var cow = new Cow();

An anonymous function is allowed to return results, just like any other function. 允许匿名函数返回结果,就像任何其他函数一样。 If you need to export items you create to the global namespace, you can just return them, like so: 如果需要将创建的项目导出到全局命名空间,则可以返回它们,如下所示:

var newglobal = (function() {
  var foo = {

   };
  return foo;
}());

This is the standard method when you are not using a package that supports the concept of exports. 当您不使用支持导出概念的包时,这是标准方法。

Alternately, if you know that you are going to run in a browser context, you also have the option of creating properties on the window object, since they are always added to the global object. 或者,如果您知道要在浏览器上下文中运行,则还可以选择在窗口对象上创建属性,因为它们始终会添加到全局对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM