如何jQuery的返回选定元素的数组 [英] how does jquery return an array of the selected elements
问题描述
我刚才看到由John Resig的,他说的jQuery作为数组操作psented一个谷歌的技术讲座$ P $。继他的意见我一直在玩一个子类阵列和它的伟大工程,但我一直在寻找通过jQuery的来源,看不到他们使用同样的方法
I have just seen a google tech talk presented by John Resig where he said jQuery operates as an array. Following that advice I have been playing with a subclassed array and it works great but I have been looking through the jQuery source and can't see that they have used the same method of
jQuery.prototype =新的Array();
和我无法看到它甚至采取与呼叫/申请或在窗口原型链中。$对象本地Array.prototype.methods,所以我想知道如何jQuery对象返回选定的数组元素。
And I can't see it even taking the native Array.prototype.methods with call/apply or in the prototype chain in the window.$ object, so I am wondering how does the jQuery object return an array of the selected elements.
我一直在使用一个普通的对象试过,但如果返回一个数组停止能够链接命令
I have tried using an ordinary object, but if returning an array it stops the ability to chain commands
如果能够取从Array.prototype一些方法中什么是必要的,以返回一个数组
If it is possible to take some methods from Array.prototype what is essential to return an array?
这是我与播放。
;(function(window, document, undefined){
function MyLib(){};
// prototype constructor functions
function Core(){};
function Methods(){};
// create new instance of the MyLib object and call the construct method
function myLib(selector){
return new MyLib().construct(selector);
}
// allow adding new methods to the prototype from the window.
// eg $.extend('module', 'name', 'function')
myLib.extend = function(module, name, fn){
if(typeof fn === 'function'){
if(!MyLib.prototype[module][name]){
MyLib.prototype[module][name] = fn;
}
} else if(typeof fn === 'object'){
for(var key in fn){
if(!MyLib.prototype[module][key]){
MyLib.prototype[module][key] = fn[key];
}
}
} else {
throw new Error("invalid type, function or objects are required");
}
}
MyLib.prototype = new Array();
MyLib.prototype.core = new Core();
MyLib.prototype.methods = new Methods();
MyLib.prototype.construct = function(selector){
var elems = document.getElementsByTagName(selector);
Array.prototype.push.apply(this, Array.prototype.slice.call(elems, 0, elems.length));
return this;
};
// access to prototype objects with $.core && $.methods
myLib.core = MyLib.prototype.core;
myLib.methods = MyLib.prototype.methods;
// give the window access to the constructor function
window.$ = myLib;
// give the window access to the prototype object for debugging
window.$$ = MyLib;
})(window, document);
// this adds a new method to the methods object within the prototype
$.extend('methods', 'test', function(){alert('method successfully added')});
// the added method can be accessed with
$.methods.test();
// or
$('tagName').test();
感谢您的答案
推荐答案
要工作作为阵列(我们通常说的类似数组的对象)不从阵列继承
,你只需要
To work "as an array" (we usually speak of "array-like" object) you don't inherit from Array
, you simply have to
- 有一个名为相关属性
长度
- 有属性
0
,1
...长度1
(你可能会跳过一些)
- have a relevant property named
length
- have properties
"0"
,"1"
...length-1
(you might skip some)
例如:
var a = {
length: 2,
"0": 'first',
"1": 'second'
}
var b = [].slice.call(a); // yes, Array functions work !
console.log(b); // logs ["first", "second"]
当然,你可以通过定义一个基于原型的类和相关的原型功能(就像jQuery不会),使这一切更容易为用户的lib:
Of course you can make all this easier for the lib users by defining a prototype based class and the relevant prototype functions (just as jQuery does) :
var A = function(){
this.length = 2;
this['0'] = 'first';
this['1'] = 'second';
}
A.prototype.slice = [].slice;
A.prototype.splice = [].splice;
var a = new A, b = a.slice();
console.log(a); // logs ["first", "second"] because of the splice function
console.log(Array.isArray(a));
console.log(b); // logs ["first", "second"] because it's really an array
console.log(Array.isArray(b)); // true
如果你想记录为数组对象,那么需要有拼接
功能。
If you want your objects to be logged as arrays, you need to have the splice
function.
这篇关于如何jQuery的返回选定元素的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!