jquery - Array Like Objects in Javascript -
i'm wondering how jquery constructs array-like object. key thing i'm trying work out how manages console interpret array , display such. know has length property, after playing bit can't quite figure out.
i know has no technical advantage on normal array object in example below. think it's important semantic element when users testing , debugging.
a normal array object.
function foo(){ // array objects have length property , it's properties use integer // based sequential key names, e.g. 0,1,2,3,4,5,6 array. this.length = 1; this[0] = 'hello' } // make sure add length property prototype match array // prototype foo.prototype.length = 0; // give array object array method test works foo.prototype.push = array.prototype.push // create array object var bar = new foo; //test bar.push('world'); console.log(bar); // outputs { 0: 'hello', 1: 'world', length: 2, __proto__: foo }
where jquery output
var jqarray = $('div') console.log(jqarray); // outputs [<div></div>,<div></div>,<div></div>,<div></div>]
if run
console.dir(jqarray) // outputs { 0: htmldivelement, 1: htmldivelement, 2: htmldivelement, 3: htmldivelement, 4: htmldivelement, context: htmldocument, length: 5, __proto__: object[0] }
the proto of jquery object interesting since object , not jquery.fn.init expected, [0] indicates when you.
console.dir([]) // outputs array[0] object name or array[x] x being internal length of // array
i have no idea how jquery has set it's proto object[0] guess answer lies somewhere in there. got ideas?
the object has have length
, splice
> var x = {length:2, '0':'foo', '1':'bar', splice:function(){}} > console.log(x); ['foo', 'bar']
and fyi, object[0]
prototype same reason. browser seeing prototype array because:
$.prototype.length == 0; $.prototype.splice == [].splice;
Comments
Post a Comment