On Jun 9, 2:34 am, David Marrs <[email protected]> wrote:
[...]
> function F(){};
> F.prototype = {foo: 'Alice', bar: 'Bob'};
>
> var a = new F();
> var b = new F();
>
> If you inspect a or b in firebug (or equivalent) you will see object
> {foo: 'Alice', bar: 'Bob'};
>
> Now try: a.foo = 'Carol';
>
> If you inspect F.prototype or b in firebug you will still see {foo:
> 'Alice', bar: 'Bob'}.
>
> Now try: F.prototype.foo = 'Dan';
>
> This time b.foo == 'Dan' while a.foo == 'Carol' still.
>
> This is what the explanation you were confused by is referring to.
> The 'new' keyword is passing an empty object[1] to variables a and b,
> plus a hidden reference to F.prototype.
[...]
>
> [1] it actually passes the function F's special 'this' object, which
> in the above example is empty

A function's this keyword is never "empty", it *always* references an
object.

It would be better to explain that the new operator causes a new
object to be assigned to F's this and that that new object has an
internal [[prototype]] property that references F.prototype.

It is this new object that is returned by default by the contsructor.


> but I shall leave the explanation of
> this, if you wish it, to a separate email.

Discussion should be here, so that anyone searching for and finding
this thread can also find the related discussion. It should lead to
better informed participants.

--
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]

Reply via email to