如果我们查看最新的jQuery源代码,可以在http://code.jquery.com/jquery-latest.js中找到以下内容:
我的理解是,在JavaScript中,
为什么会这样呢?我的理解是否有误?
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
}
我的理解是,在JavaScript中,
new
关键字会传递一个空对象{}
给函数,并通过this.blah
在这个对象上设置一些东西。此外,我认为new
与.call
/.apply
等不同之处在于返回的对象也具有与函数相同的原型。因此,返回值应该具有与jQuery.prototype.init.prototype
(或jQuery.fn.init.prototype
)相同的原型。但是,从我所看到的,它的原型设置为jQuery.prototype
,因此所有可用于操作集合的命令都可以使用。为什么会这样呢?我的理解是否有误?