If you're using Firefox and Firebug, or Chrome or Safari, instead of
alert user console.log:
var ob = { prop: 1 };
var obCopy = Object.create(ob);
console.log(ob);
console.log(obCopy);
This will give you a much better view of your object internals.
Anyway, what's happening here is that obCopy doesn't have a property
of its own called prop - that's held by its prototype. When you call
obCopy.prop, JavaScript is looking up the prototype chain until it
finds prop.
JSON.stringify doesn't show properties that aren't the objects *own*
properties, because if it did it would have to stringify all the
properties of Object.
Hope that helps!
Nick
On 9 July 2011 13:41, Ivan S <[email protected]> wrote:
> Hi all.
>
>
> Can someone explain to me why this code:
>
> var ob = { prop: 1 };
>
> var obCopy = Object.create(ob);
>
> window.alert(JSON.stringify(ob));
>
> window.alert(JSON.stringify(obCopy));
>
> produces output: "{ prop: 1 }" and "{}"? I don't understand why "obCopy"
> isn't identical as "ob", because "obCopy.prop" is equal to 1.
>
> --
> 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]
>
--
Nick Morgan
http://skilldrick.co.uk
@skilldrick
Save our in-boxes! http://emailcharter.org
--
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]