jQuery 如何快速克隆其方法?
How does jQuery clone its methods so quickly?
我正在尝试 return 类似对象的查询,在我的第一次尝试中,我尝试了 Object.create
方法
var ElementArray = {
someMethod : myMethod,
....
}
var addMethods = function(elements) {
var obj = Object.create(ElementArray);
obj[0] = elements;
return obj;
};
var getId = function( query ) {
return addMethods( doc.getElementById(query) );
};
我立即发现这比 jQuery(嘶嘶声)慢,尤其是在 firefox 上。 firefox 的问题可能与跨分区包装器有关 (see bug here),但我仍然很不满意。
我也试过用原型代替
var ElementArray = function(){};
ElementArray.prototype.someMethod = someMethod;
....
var addMethods = function(elements) {
var obj = new ElementArray();
....
};
在 Chome 上稍微好一点,但在 Firefox 上仍然很慢。
所以我的问题是,jQuery(sizzle) 和其他库是如何做到的 || return 具有 1-2 个实例属性的对象的最快方法是什么? (其他仅供参考)
So my question is, how does jQuery(sizzle), and other libraries do it
jQuery 使用原型。它通过将原型别名为 .fn
来隐藏这一事实,但它仍然是原型。这里是 the jQuery function.
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new jQuery.fn.init( selector, context );
},
还有,这里是 the aliasing:
jQuery.fn = jQuery.prototype
并且,执行the actual jQuery constructor:
init = jQuery.fn.init = function( selector, context, root ) {
var match, elem;
// HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
}
// Method init() accepts an alternate rootjQuery
// so migrate can support jQuery.sub (gh-2101)
root = root || rootjQuery;
.....
而且,迂回assignment of the `.init.prototype':
// Give the init function the jQuery prototype for later instantiation
init.prototype = jQuery.fn;
我正在尝试 return 类似对象的查询,在我的第一次尝试中,我尝试了 Object.create
方法
var ElementArray = {
someMethod : myMethod,
....
}
var addMethods = function(elements) {
var obj = Object.create(ElementArray);
obj[0] = elements;
return obj;
};
var getId = function( query ) {
return addMethods( doc.getElementById(query) );
};
我立即发现这比 jQuery(嘶嘶声)慢,尤其是在 firefox 上。 firefox 的问题可能与跨分区包装器有关 (see bug here),但我仍然很不满意。
我也试过用原型代替
var ElementArray = function(){};
ElementArray.prototype.someMethod = someMethod;
....
var addMethods = function(elements) {
var obj = new ElementArray();
....
};
在 Chome 上稍微好一点,但在 Firefox 上仍然很慢。
所以我的问题是,jQuery(sizzle) 和其他库是如何做到的 || return 具有 1-2 个实例属性的对象的最快方法是什么? (其他仅供参考)
So my question is, how does jQuery(sizzle), and other libraries do it
jQuery 使用原型。它通过将原型别名为 .fn
来隐藏这一事实,但它仍然是原型。这里是 the jQuery function.
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new jQuery.fn.init( selector, context );
},
还有,这里是 the aliasing:
jQuery.fn = jQuery.prototype
并且,执行the actual jQuery constructor:
init = jQuery.fn.init = function( selector, context, root ) {
var match, elem;
// HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
}
// Method init() accepts an alternate rootjQuery
// so migrate can support jQuery.sub (gh-2101)
root = root || rootjQuery;
.....
而且,迂回assignment of the `.init.prototype':
// Give the init function the jQuery prototype for later instantiation
init.prototype = jQuery.fn;