JQuery库模块导出 [英] JQuery library module export

查看:97
本文介绍了JQuery库模块导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解jQuery如何设置自己。

I am trying to understand how jQuery sets itself up.

在开始时,jQuery会自动调用一个导出模块的函数。

Right at the beginning jQuery automatically calls a function, which exports a module.

设置如何运作?

这里有一些更详细的子问题可能会回答更一般的问题:

Here some more detailed sub-questions which might answer the the more general question:


  • 函数(w)有什么用? > module.exports ?

  • noGlobal 变量的用途是什么?

  • 实际设置的工厂在哪里?它的类型是什么?

  • 为什么 factory 参数是用一个参数和两个参数调用的吗?

  • 什么是全局论据应该包含哪些? (我希望有类似c ++的类型......)

  • What is the use of the recursive call to function(w) at module.exports?
  • What is the use of the noGlobal variable?
  • Where is the factory actually set up and what is its type?
  • Why can the factory argument get called with one argument and with two as well?
  • What is the global argument supposed to contain? (I wish there were a type like in c++...)
(function( global, factory ) {

    if ( typeof module === "object" && typeof module.exports === "object" ) {
        // For CommonJS and CommonJS-like environments where a proper `window`
        // is present, execute the factory and get jQuery.
        // For environments that do not have a `window` with a `document`
        // (such as Node.js), expose a factory as module.exports.
        // This accentuates the need for the creation of a real `window`.
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info.
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

    // Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {


推荐答案


有什么用?递归调用函数(w) module.exports

它不是递归调用,更像是延迟初始化函数。在一些CommonJS环境中,如Node.JS,全局对象没有文档属性,而其他如Browserify和Webpack则可以。

It isn't a recursive call, more of a deferred initialization function. In some CommonJS environments, like Node.JS, the global object does not have a document property, while others such as Browserify and Webpack do.

jQuery需要文档要初始化的属性,所以首先检查全局对象是否包含文档属性。如果是,则立即初始化,使浏览器内的CommonJS环境感到高兴。它没有,它返回一个函数,可以用来稍后初始化jQuery。这个函数以后可以在假胜利上调用dow,用jsdom之类的东西创建。

jQuery requires the document property to initialize, so it first check to see if the global object contains a document property. If it does, it initializes immediately, making in-browser CommonJS environments happy. If it does not, it returns a function that can be used to later initialize jQuery. This function could later be called on a fake window, creating with something like jsdom.


<$ c有什么用? $ c> noGlobal 变量?

noGlobal 变量在这里使用。

摘自jQuery

Excerpt from jQuery:

// Expose jQuery and $ identifiers, even in
// AMD (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if ( typeof noGlobal === strundefined ) {
    window.jQuery = window.$ = jQuery;
}

基本上,如果 noGlobal undefined ,jQuery会将自己添加到全局窗口对象中。唯一不会这样做的是它是由CommonJS加载器加载的,在全局对象上有一个 document 属性,比如Browserify或Webpack。以下调用是 noGlobal 不是未定义

Essentially, if noGlobal is undefined, jQuery will add itself to the global window object. The only time it will not do this is if it is loaded by a CommonJS loader, with a document property on the global object, such as Browserify or Webpack. The call below is where noGlobal is not undefined.

factory( global, true )








实际设置的 factory 在哪里?它的类型是什么?

Where is the factory actually set up and what is its type?

factory 变量是函数,并在此处声明:

The factory variable is a function, and it is declared here:

function( window, noGlobal ) {

这是传递给 IIFE


为什么工厂用一个参数和两个参数调用参数?

Why can the factory argument get called with one argument and with two as well?

因为JavaScript。

Because JavaScript.

在JavaScript中,不需要匹配声明函数的参数个数。任何省略的参数都具有值 undefined

In JavaScript, there is no requirement to match the number of arguments a function is declared with. Any omitted arguments have the value undefined.


什么是全局参数应该包含什么? (我希望有类似c ++的类型......)

What is the global argument supposed to contain? (I wish there were a type like in c++...)

它应该包含JavaScript环境的全局对象。在浏览器中,此对象称为窗口,在Node中,此对象称为 global 。在这两种环境中,使用全局范围内的此将解析为全局对象,无论它的全局名称是什么。

It is supposed to contain the global object for the JavaScript environment. In a browser, this object is known as window, and in Node, this object is known as global. In both environments, using this in the global scope will resolve to the global object, whatever it's global name is.

但是,由于某些第三方包装器可以更改jQuery初始化的范围,jQuery将首先检查窗口对象是否可用,如果是,则使用它。如果不使用,它将默认使用

However, due to some 3rd-party wrappers which can change the scope in which jQuery is initialized, jQuery will first check if the window object is available and use it if it is. If not use, it will default to using this.

typeof window !== "undefined" ? window : this








还有一个问题:w参数来自哪里?

one more question: where is the w argument coming from?

当全局对象不包含文档时,它返回一个接受一个参数的函数, w 。这个对象是一个窗口 -like对象,带有文档,可以用jsdom之类的东西创建。

When the global object does not contain a document, it returns a function that accepts one argument, the w. This object would be a window-like object with a document that can be created with something like jsdom.

这篇关于JQuery库模块导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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