How to customize jquery based javascript API assembly and some syntax questions used in jQuery

I come here with a lot of questions, so let's get started:

I want to know some things about the syntax used to create jquery, since I want to learn from it how to use it for myself.

Question 1: Getting jQuery library


(function( window, undefined ) {

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context );
    },

    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery, .......

I would like to know what is meant by the parenthesis before such a function "(function (window ..." so far I have only declared my function like this


function myFunc(myArg1,myArg2){
//stuff
}

Question 2:

At the end of the jquery library, I seem to understand that the $ sign is assigned in the global scope, so we can use $ any for selectors, which I don’t understand is what "(window);" does expression at the very end means and what purpose it serves.


        };

});
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

})(window);

: javascript-, , : "ds.functionName(Arg1)"; , JQuery $

: D

+3
3

1 2 . , , (function(params){...}) (window), . , , . function foo(){...}, , foo - (), . , , function foo , foo. , :-) 188663 .

, jQuery .

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

, jQuery $, " ", . , - 1479319

+2

1) , , , , .

2) window window , function( window, undefined ) { , .

+1

What you are looking at is called an anonymous function. Anonymous functions can be passed as regular variables. They are also executed immediately after their announcement. This is a pretty paradigm in understanding this concept. I am sure there are several other explanations here in Stackoverflow that can explain them.

(...your code...)(argument);

So in the second example:

});
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

})(window);

the argument windowis passed to the anonymous function launched in example 1

+1
source

Source: https://habr.com/ru/post/1759538/


All Articles