es-discuss Digest, Vol 52, Issue 117

Axel Rauschmayer axel at rauschma.de
Mon Jun 20 13:27:39 PDT 2011


Terminology (created by me, but I think it explains well what is going on):

- |this| points to the object where property lookup starts. It always points to the beginning of the prototype chain.
- |here| points to the object where a property was found. |here| can point to any object in the prototype chain.

|super| starts property lookup in the prototype of |here|, but does not change |this|. That is, a method invoked via |super| still has the same |this| and property lookup via |this| is unchanged. If the super-method again uses |super|, then property lookup will begin in the prototype of |here| (= where the super-method has been found). Etc.

> From: Mariusz Nowak <medikoo+mozilla.org at medikoo.com>
> Date: June 20, 2011 13:49:20 GMT+02:00
> Subject: Re: Making "super" work outside a literal?
> 
> It's most sane proposal I think. However few things are not obvious to me,
> will following evaluate as I assume:
> 
> var A = {
> 	one: function () {
> 		return 'A.foo';
> 	}
> };
> var B = Object.create(A);
> 
> var C = Object.create(B);
> C.one = function () {
> 	return super.one();
> };
> 
> var c1 = Object.create(C);
> obj.one(); // 'A.foo'

That would be c1.one(), right?

|here| === C and thus the search for super.one starts in B and finds A.one.

> B.two = function () {
> 	this.three();
> };
> B.three =  function () {
> 	return 'B.three';
> };
> C.two = function () {
> 	super.two();
> };
> C.three = function () {
> 	return 'C.three';
> };
> 
> var c2 = Object.create(C);
> c2.two();  // C.three

|here| === C and thus super.two === B.two

B.two() uses the original and unchanged |this| which is still c2.
=> this.three === C.three

SUMMARY: I agree with your findings.

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110620/03329717/attachment.html>


More information about the es-discuss mailing list