Я видел этот код (объяснительно это в jQuery, с модификацией)
(function(window,undefined){
var jQuery=(function(){
var jQuery=something;
jQuery.xxx=xxx;
//...
return jQuery;
})();
//...
window.jQuery=window.$=jQuery;
})(window);
Пока я понимаю, что код упаковки во встроенном вызове функции может четко определять область переменной, я не понимаю преимуществ
- передать
window
с помощью параметра вместо прямого использования, - получить экземпляр
undefined
с помощью параметра undefined, а также - определение
jQuery
возвращаемым значением другого встроенного вызова функции. Может кто-нибудь объяснить немного?
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);