html - Arrays with 'named' constructors in JavaScript? (e.g HTMLCollection) -
consider following 2 ways of creating instance of object:
function f() { return { k: 'v'} } var inst1 = f(); function f() { this.k = 'v'; } var inst2 = new f();
the behavior of inst1 , inst2 same, difference different constructor saved object:
inst1.constructor; // object() inst2.constructor; // f()
what's constructor of array? see:
var xs = []; xs.constructor; // array()
until point understand logic. bumped following:
var tags = document.getelementsbytagname("*"); typeof tags; // object tags.constructor; // htmlcollection()
so far it's similar inst2 example. when console.log firebug, receive array 'named' constructor:
console.log(tags); // htmlcollection[tag1, tag2, ...]
the block brackets confuse me, have expected curly 1 here. there must explanation this, knows answer?
it sounds crux of question lies in determining how firebug displays object. firebug examines objects passed it, , chooses how display objects string based on properties have.
htmlcollection "array-like" has length property, , stores contents in properties named 0, 1, 2, etc. makes array-like , firebug recognizes this, outputting string representation of object array.
in case of firebug, if sees length
, splice
property, treat object array-like:
var myarraylike = function(){ this[0] = 1; this.length = 1; this.splice = function(){}; }
firebug output:
-> new myarraylike(); <- [1]
Comments
Post a Comment