Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a question about parent & prototype

12 views
Skip to first unread message

Lu Wenzhe

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
Hi all,
I have a stupid question about jsenging.
What is a parent object?
And what is a prototype object?
And what is the diffirence between them?

Thx a lot


卢文哲

unread,
Apr 4, 2000, 3:00:00 AM4/4/00
to
I really need the answers of the question,
Please help me if you can.
Thx

Brendan Eich

unread,
Apr 4, 2000, 3:00:00 AM4/4/00
to ¬ÎÄÕÜ
Briefly, the [[Parent]] internal property, called [[Scope]] and specified only for function objects in ECMA ed. 3, carries the scope chain link.  The [[Prototype]] internal property carries the "delegation link" for explicitly qualified property references.  Since JS1.2, [[Parent]] has been reflected as a property, __parent__, and [[Prototype]] as __proto__.  So, for example:

  o = {p: 42};
  x = {__proto__: o};
  print(x.p);      // prints 42, delegated from x to o, which is x's prototype
  x.p = 43;        // o.p is still 42, but now x has a 'p' property hiding it

[[Parent]] is harder to see in action, especially as the __parent__ reflection property cannot be set (for security and safety reasons).  The [[Parent]] link plays a crucial role, implementing static scoping rather than dynamic (caller) scoping, when there are multiple "global" or "top-level" objects, as in the HTML DOM:

In a script tag in foo.html, loaded into window w1:
  var x = 3;
  function f() { alert(x) }

In another script tag in bar.html, loaded into window w2:
  var x = 4;
  w1.f();

Per years of backward compatibility, dating from Netscape 2.0, and now per ECMA ed. 3 if you extend it to allow multiple "global objects" (Window objects), the call w1.f() in the second script must alert 3, not 4 (as it would if JS used dynamic scoping, where the caller's scope is used to parent the callee's activation).

The right x variable is found via the [[Scope]], also known in our engine as [[Parent]], internal property of the function object created for function f() {alert(x)} when the compiler processed that source function statement from foo.html.  At that time, the compiler stored the window object reference w1 as the value of f's [[Parent]] property.

Later, when the script from bar.html called w1.f(), an activation object was created to scope f's invocation, and its scope chain link (again, [[Parent]]) was copied from f's [[Parent]].  The scope chain for the execution environment used during this invocation points to the activation object.  One could diagram that scope chain thus:

  scope-chain -[[Parent]]-> activation-for-f -[[Parent]]-> w1

Another example of [[Parent]] arises in the DOM's intrinsic event handlers, e.g.

  <form id="myForm">
  <input type="text" name="myText" value="">
  <input type="button" name="myButton" onclick="myText.value = 'Hi!'">
  </form>

Notice that the onclick attribute's value refers to myText without any document.myForm. qualification to the left.  The identifier myText is resolved by search the scope chain of the execution environment used when onclick is invoked.  That scope chain looks like this (with --> standing for the -[[Parent]]-> arrow that I used above):

  scope-chain --> activation-for-onclick --> myButton --> myForm --> document --> window

This deep scope chain allows intrinsic event handlers to use brief names, where unambiguous, for properties in the objects that enclose the event handler attribute's target object.

/be

solvea...@my-deja.com

unread,
Apr 5, 2000, 3:00:00 AM4/5/00
to
In article <38E93845...@cnnic.net.cn>,

=?gb2312?B?wqzOxNXc?= <l...@cnnic.net.cn> wrote:
> > I have a stupid question about jsenging.
> > What is a parent object?
> > And what is a prototype object?
> > And what is the diffirence between them?
see http://wsabstract.com/javatutors/proto.shtml

Hope it helps
BugsB


Sent via Deja.com http://www.deja.com/
Before you buy.

Lu Wenzhe

unread,
Apr 5, 2000, 3:00:00 AM4/5/00
to
Thanks a lot!
You are welcome!

Lu

0 new messages