简体   繁体   English

用es6学习js

[英]learning js with es6

I have the following simple code: 我有以下简单的代码:

window.Foo = window.Foo || {};

((Foo) => {
  'use strict';

  Foo.select = Foo.select || {};

  Foo.select.init = (selector, options) => {
    ...
  };

  Foo.select.otherInit = () => {
    ...
  };

  $((e) => {
    Foo.select.init();
    Foo.select.otherInit();
  });

})(window.Foo);

As I am a js principiant I wonder a couple of things: 由于我是js原理,所以我想知道以下几点:

First the stupid one: instead of passing Foo can I access it by this.Foo inside the function? 首先是一个愚蠢的人:我可以通过this.Foo在函数内部访问它而不是传递Foo As I guess so, is it just kind of code style? 我猜想,这只是一种代码样式吗?

what does (window.Foo) in the last line mean? 最后一行(window.Foo)是什么意思? what's for? 有什么用

Lastly, why using: 最后,为什么使用:

$((e) => {
  Foo.select.init();
  Foo.select.otherInit();
});

and not just: 而不仅仅是:

Foo.select.init();
Foo.select.otherInit();

thank you! 谢谢!

First the stupid one: instead of passing Foo can I access it by this.Foo inside the function? 首先是一个愚蠢的人:我可以通过this.Foo在函数内部访问它而不是传递Foo? As I guess so, is it just kind of code style? 我猜想,这只是一种代码样式吗?

Yes, you could access it directly through this.Foo , but only because the current function-scope is bound to the window Object (because you are using an arrow function ) and because Foo is part of window (possibly due to the fact that it was just defined in the global scope). 是的,您可以直接通过this.Foo访问它,但这仅是因为当前功能范围已绑定到window Object(因为您使用的是箭头功能 ),并且因为Foowindow一部分(可能是因为它只是在全局范围内定义)。

If you were to try this in a function call inside another object, this.Foo would fail. 如果要在另一个对象内的函数调用中尝试此操作,则this.Foo将失败。 I would not recommend doing this, unless you have a good reason to do it this way. 我不建议您这样做,除非您有充分的理由这样做。

Here can learn more about arrow functions and the this context. 在这里可以了解有关箭头功能和this上下文的更多信息。

what does (window.Foo) in the last line mean? 最后一行(window.Foo)是什么意思? what's for? 有什么用

Your whole construct (see below) is an IIFE : Immediately-invoked function expression. 您的整个构造(请参见下文)是一个IIFE :立即调用的函数表达式。 You are using an arrow function, so this binds the context as well (see point above). 您正在使用箭头功能,因此它也绑定了上下文(请参见上文)。

The last part, (window.Foo) , means that you invoke the function you called with the argument window.Foo , which is then available as Foo within your function. 最后一部分(window.Foo)意味着您使用参数window.Foo调用了所调用的函数,该函数随后在函数中以Foo提供。

((Foo) => {
    ...
})(window.Foo);

Lastly, why using: 最后,为什么使用:

$((e) => { Foo.select.init(); Foo.select.otherInit(); }); $((e)=> {Foo.select.init(); Foo.select.otherInit();});

and not just: 而不仅仅是:

Foo.select.init(); Foo.select.init(); Foo.select.otherInit(); Foo.select.otherInit();

This is a jQuery function call, which is executed once the DOM is ready . 这是一个jQuery函数调用,在DOM准备就绪后执行 The function within $(...) is called, once the DOMContentLoaded event is fired. 触发DOMContentLoaded事件后,将调用$(...)的函数。

If this were not present, the code could be executed before the DOM is ready (thus possibly not finding the DOM elements referenced in those functions). 如果不存在,则可以在DOM准备就绪之前执行代码(因此可能找不到那些函数中引用的DOM元素)。 This depends on where you place your javascript code though (if you place it right before </body> you should be safe). 不过,这取决于您将JavaScript代码放置在哪里(如果您将其放置在</body>之前),则应该是安全的。

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

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