Hey.

Question by jQuery.

  1. What window.jQuery() object is obtained at the output of the window.jQuery() function and where is this object "attached" to the BOM?

  2. Why is the new keyword not used? As far as I know, only the constructor function can create an object. And window.jQuery() does just that - creates an object. In the Flanagan book, it is written about this that "jQuery () is a factory function, not a constructor: it returns a newly created object, but it is used without the keyword new." What does the "factory" function mean? The first time I see such a term.

    2 answers 2

    The output window.jQuery () is a regular javascript object. It is not attached anywhere. You can save it in any variable, or pass it as a parameter.

    Why is the new keyword not used?

    Because jQuery is a regular function, not a constructor.

    The factory function is also known as the Factory Method.

    To find out what happens in the jQuery function, you can refer directly to the sources, and there you can see the following

     function (selector, context) { // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery.fn.init(selector, context); } 

    This shows that this function is simply used to call the constructor, so there is no need to use the new operator

      According to the docks, the argument can be a string, a DOM object, a DOM collection, a js object, a jquery object, or nothing.

      https://api.jquery.com/jQuery/