On Jul 20, 9:49 pm, Pete Otaqui <[email protected]> wrote:
> This isn't really to do with arrays, but does demonstrate that you can't
> change the .prototype of an "instance" ...
>
> http://jsbin.com/onomic/4/edit
If you are going to post example code, please post it here in a form
that can be copy and pasted easily.
Array2 = function() { };
Array2.prototype = [];
Array2.prototype.any = function(testFunction) {
var i, len;
for ( i=0, len=this.length; i<len; i++ ) {
if ( testFunction(this[i], this) ) {
return true;
}
}
return false;
};
var myArray2 = new Array2();
myArray2.push('foo');
myArray2.push('bar');
myArray2.push('baz');
console.log( myArray2.any(function(item, list) { return
item==='bar'; }) ); // true
It shows "false" in IE 6.
console.log( myArray2.any(function(item, list) { return
item==='eck'; }) ); // false
myArray2.prototype = [];
console.log(typeof myArray2.any); // function
What did you expect? The assignment of some value to a prototype
property of myArray2 will not have any effect on resolution of
identifiers on its [[Protoype]] chain.
console.log(Array2.prototype === myArray2.__proto__); // true
So the __proto__ property is referencing the constructor's prototype,
which is expected.
myArray2.__proto__ = [];
console.log(typeof myArray2.any); // undefined
You just replaced myArra2[[Prototype]] with an object that doesn't
have an *any* property.
More to the point is:
console.log(Array2.prototype.any); // function
and
console.log(Array2.prototype === myArray2.__proto__); // false
Which shows that myArray2[[Prototype]] now points to a completely
different object to Array.protoype (which was its original
[[Prototype]] object).
So in browsers that support Mozilla's __proto__ property, you *can*
change the [[Prototype]] of an instance using __proto__.
--
Rob
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]