您正在询问两种不同类型的对象。
$是相同的,jQuery并且是一个具有属性的函数。 $.ajax是这些属性之一。
创建的实际 jQuery 对象$('body')实际上是一个对象,它是jQuery.fn.initnot of 的一个实例jQuery。
所以,这是你看到不同方法的第一个原因,$因为$('body')它们是不同类型的对象,因此可以有不同类型的方法。
为了进一步理解,方法 on $(它是 的同义词jQuery)是直接添加到jQuery函数本身的方法。在 jQuery 代码中,这主要是jQuery.extend()在 jQuery 对象上完成的。 $.ajax就是其中之一。
jQuery 函数创建的 jQuery 对象上的方法是分配给的方法,jQuery.fn.init.prototype由于 jQuery 的一些诡计,它们是分配给jQuery.fn. 事实证明,当任何东西被分配给 时,它jQuery.fn.init.prototype被设置为同一个对象,它会自动转到并且该原型上的任何东西都会自动成为对象的方法,该对象是由 jQuery 函数创建的,例如or 。jQuery.fnjQuery.fnjQuery.fn.init.prototypejQuery.fn.initjQuery('body')$('body')
您可以在 jQuery 代码中看到这一点。如果您查看 jQuery 函数,它看起来像这样(它创建一个对象,jQuery.fn.init因此将具有来自的方法jQuery.fn.init.prototype:
// 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, rootjQuery );
},
而且,后来.fn是这样的:
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
这就是分配给的任何方法也是如何jQuery.fn在jQuery.fn.init.prototypejQuery 对象上成为方法的。