看看旧版jQuery版本的结构:
(function( window, undefined ) {
var jQuery = (function() {
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context );
};
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
// ...
return this;
}
// jQuery API methods
}
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
return (window.jQuery = window.$ = jQuery);
})();
})(window);
Run Code Online (Sandbox Code Playgroud)
很容易理解:
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
return this;
}
}
jQuery.fn.init.prototype = jQuery.fn;
Run Code Online (Sandbox Code Playgroud)
是例如在正常使用时调用jQuery时触发的部分
$('p')
Run Code Online (Sandbox Code Playgroud)
要么
jQuery('p')
Run Code Online (Sandbox Code Playgroud)
目前还不清楚如何使用表单调用API方法,$.ajax()或者$.isArray()在哪里放置,例如,在列出的代码中使用自定义方法并使用$ .myCustomMethod()调用它.任何帮助将非常感谢.
你可以自己写一个函数:
function something(n) {
return n + 1;
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
var x = something(0);
Run Code Online (Sandbox Code Playgroud)
您还可以将属性附加到函数,因为它是一个对象:
something.else = function(n) {
return n - 1;
};
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过属性引用调用该函数:
var y = something.else(0);
Run Code Online (Sandbox Code Playgroud)
jQuery库使用全局jQuery对象(通常称为$)作为放置一组函数的地方,这些函数通常很有用,但是对于元素或对象的集合没有任何隐式关系,各种jQuery"方法"做.一个明显的例子是$.ajax(),它非常有用,但实际上与DOM没有任何关系.
通过将所有这些函数保留为属性$,库避免"污染"全局地址空间.