В чем преимущество вызовов встроенных функций?
Я видел этот код (точно он в jQuery, с модификацией)
(function(window,undefined){
var jQuery=(function(){
var jQuery=something;
jQuery.xxx=xxx;
//...
return jQuery;
})();
//...
window.jQuery=window.$=jQuery;
})(window);
Хотя я понимаю, что перенос кода во встроенном вызове функции может четко определить область действия переменной, я не понимаю преимуществ
passingwindow
with a parameter instead of using it directly,
getting an instance of undefined
by a undefined parameter, and also
defining jQuery
by the return value of another inline function call.
Can somebody explain a bit?
EDIT напишите # 3 более четко:
Я понимаю, что код определяетjQuery
внутри другой функции затем верните ее.
//(function(window,undefined){
var jQuery=(function(){
// Inside this function defines jQuery and return it?
var jQuery=function(selector,context){
return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
return jQuery;
})(); // This executes the inline function and assign `jQuery` with the return value???
//... })(window);
Это больше похоже на следующее:
function define_jQuery(){
// Inside this function defines jQuery and return it?
var jQuery=function(selector,context){
return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
return jQuery;
}
//(function(window,undefined){
var jQuery=define_jQuery(); // This executes the inline function and assign `jQuery` with the return value???
//... })(window);
Не было бы проще сделать:
//(function(window,undefined){
var jQuery=function(selector,context){
return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
//... })(window);