Yeah right... I got it...
I've always thought that the instances were related to the Constructor
forever because :
function C(){}
var c = new C();
C.prototype.p = 'v';
c.p;//'v'
But in fact it's just because they have a reference to the same
object... so if you make the reference point to anither object :
C.prototype = { p: 'v2' };
They won't be linked anymore...
c.p;//'v'
Thanks for pointing that out :)
2011/7/16 António Afonso <[email protected]>:
> On Jul 14, 3:03 pm, xavierm02 <[email protected]> wrote:
>> Hi,
>>
>> http://jsbin.com/ejodoq/edit
>> http://jsbin.com/uqoyep/edit
>> http://jsbin.com/opinus/edit
>> http://jsbin.com/ejufih/edit
>
>>I thought setting the prototype of a constructor was just like cycling
>>through instances and change their __proto__...
>
> The __proto__ is set to the Func.prototype at the time of new Func().
> When you change Func.prototype to be some other object there's nothing
> the "instances" can do about it.
>
> It's not different than:
> var A = {};
> var b = {linkToA: A};
> A = {id: 1};
>
> Just because A points to a different object now, it doesn't make
> b.linkToA to point to that same object too.
>
> So when the .prototype of a function is assigned to another object
> it's not going to affect the prototype chain of any object, unlike
> changing the __proto__.
>
> Here's a modified version of your code isolating the issue with only
> one function: http://jsbin.com/ecufiz/3/edit
>
>>I was wondering why it would silently fail in the first case and throw an
>>error in all the others...
>
> It's not failing like the others because there's no cycle on the
> prototype chain.
> "A.prototype instanceof B" checks B.prototype against each object of
> "A.prototype" prototype chain. Since you've changed B.prototype to
> something else it will never be found in the chain, hence returning
> false.
>
> Hope it's not confusing, It would be easier to explain with a
> drawing..
>
> Here's some more info:
> 13.2.2 [[Construct]] http://bclary.com/2004/11/07/#a-13.2.2
> 15.3.5.3 [[HasInstance]](V) http://bclary.com/2004/11/07/#a-15.3.5.3
>
> --
> 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]
>
--
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]