473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

prototype __proto__ super and delegation

Hello JS Gurus,

One thing I haven't figured out about javascript is the treatment of
__proto__.

Inheritence, whether prototypes or class-based, is just a shorthand
form of delegation (leaving aside dynamic dispatch).

In Java a derived class serves as a wrapper for its superclass. The
derived class can easily access members of its super class using the
nice "super" keyword. Why, in javascript, is this functionality buried
in the unofficial "__proto__" property? It seems that it must have
been the intent of the language designer that explicitly accessing the
prototype chain is a bad thing for some reason, although I can't see
any reason.

By googling, you can find a lot of weird hacks to add "super"-like
functionality to javascript, although many of them look misguided to
me. It certainly seems that this is a commonly confusing part of the
language.

If __proto__ were an official part of the language, implementing a
decorator or proxy pattern would be nicely simplified compared to
class-based inheritence. The "before-and-after" style of AOP (which is
just another style of shorthand for delegation) would be possible
without any crazy tricks.

So, anyone care to set me straight? Why isn't __proto__ or something
like it a well defined part of javascript? Shouldn't it be??

Thanks,

-chris

Oct 29 '07 #1
6 3411
On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
cbare wrote:
If __proto__ were an official part of the language, implementing a
decorator or proxy pattern would be nicely simplified compared to
class-based inheritence. The "before-and-after" style of AOP (which is

What do you mean by AOP?
Aspect-oriented programming. It's what's cool these days. It the
extreme opposite of having single DOM0 event handler. It is all about
decoupling concerns.

just another style of shorthand for delegation) would be possible
without any crazy tricks.
So, anyone care to set me straight? Why isn't __proto__ or something
like it a well defined part of javascript?
It should be part of the language. The Self language has an
interesting way of assigning multiple parent objects (aka prototypes.)
If this was possible in JavaScript it might be a nice way to have
multiple inheritance. Certainly in a prototype-based language I would
imagine that there would be more standard language-level support for
manipulating the prototype(s) of an object.
It is a well-defined part of JavaScript. It is an implementation-dependent
extension of ECMAScript, though. But that is so for several JavaScript
features, especially lately.
Shouldn't it be??
I think so.

Why, `.__proto__' is but a JavaScript shortcut for the ECMAScript-defined
`.constructor.prototype'.
obj.__proto__ is not a short cut for obj.constructor.prototype. For
example...


var adam = {
name: 'Adam',
speak: function() {alert('get away from that snake');}
};

var eve = {
name: 'Eve',
speak: function() {alert('that apple sure looks juice');}
};

function Person(name) {
this.name = name;
}
Person.prototype = adam;
// repair
Person.prototype.constructor = Person;

var cain = new Person('Cain');
cain.speak(); // like adam
// sex change
cain.__proto__ = eve;
cain.speak(); // NOW like eve

var able = new Person('Able');
able.speak(); // like adam
//(Cain's sex change had no affect
// on people created in the future)
able.constructor.prototype = eve;
able.speak(); // STILL like adam

var mary = new Person('Mary');
mary.speak(); // like eve
Peter

Oct 30 '07 #2
Peter Michaux wrote:
On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>cbare wrote:
>>If __proto__ were an official part of the language, implementing a
decorator or proxy pattern would be nicely simplified compared to
class-based inheritence. The "before-and-after" style of AOP (which is
What do you mean by AOP?

Aspect-oriented programming. It's what's cool these days.
That must be it. I have never had much interest in hypes.
It the extreme opposite of having single DOM0 event handler.
And what would a "DOM0 event handler" be?
It is all about decoupling concerns.
So AOP is nothing more than a synonym for the misguided "Unobtrusive
JavaScript" hype?
>Why, `.__proto__' is but a JavaScript shortcut for the ECMAScript-defined
`.constructor.prototype'.

obj.__proto__ is not a short cut for obj.constructor.prototype.
I think it is if you set up the prototype chain properly. I will look into
this later.
PointedEars
Oct 30 '07 #3
On Oct 30, 2:52 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Peter Michaux wrote:
On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
cbare wrote:
If __proto__ were an official part of the language, implementing a
decorator or proxy pattern would be nicely simplified compared to
class-based inheritence. The "before-and-after" style of AOP (which is
What do you mean by AOP?
Aspect-oriented programming. It's what's cool these days.

That must be it. I have never had much interest in hypes.
Ignore the hype. APO is useful.

It the extreme opposite of having single DOM0 event handler.

And what would a "DOM0 event handler" be?
<body onload="alert('an explicit example of DOM0 event handler');">

It is all about decoupling concerns.

So AOP is nothing more than a synonym for the misguided "Unobtrusive
JavaScript" hype?
No. Nothing to do with JavaScript.

http://en.wikipedia.org/wiki/Aspect-...ed_programming

Why, `.__proto__' is but a JavaScript shortcut for the ECMAScript-defined
`.constructor.prototype'.
obj.__proto__ is not a short cut for obj.constructor.prototype.

I think it is if you set up the prototype chain properly. I will look into
this later.
One has to do with constructing new objects. The other with already-
constructed objects.

Peter

Oct 30 '07 #4
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>Peter Michaux wrote:
>>On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
cbare wrote:
If __proto__ were an official part of the language, implementing a
decorator or proxy pattern would be nicely simplified compared to
class-based inheritence. The "before-and-after" style of AOP (which is
What do you mean by AOP?
Aspect-oriented programming. It's what's cool these days.
That must be it. I have never had much interest in hypes.

Ignore the hype. APO is useful.
That remains to be seen.
>>It the extreme opposite of having single DOM0 event handler.
And what would a "DOM0 event handler" be?

<body onload="alert('an explicit example of DOM0 event handler');">
This is not at all a "DOM0 event handler". It is a standardized attribute
(of HTML 4.01) that provides listener code for the equally standardized
`load' event (of DOM Level 2 Events).
>>It is all about decoupling concerns.
So AOP is nothing more than a synonym for the misguided "Unobtrusive
JavaScript" hype?

No. Nothing to do with JavaScript.

http://en.wikipedia.org/wiki/Aspect-...ed_programming
Thanks for the pointer; I would probably have found that shortly after, though.
>>>Why, `.__proto__' is but a JavaScript shortcut for the ECMAScript-defined
`.constructor.prototype'.
obj.__proto__ is not a short cut for obj.constructor.prototype.
I think it is if you set up the prototype chain properly. I will look into
this later.

One has to do with constructing new objects. The other with already-
constructed objects.
Your statement does not make sense.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Oct 30 '07 #5
On Mon, 29 Oct 2007 at 22:45:18, in comp.lang.javascript, cbare wrote:

<snip>
>So, anyone care to set me straight? Why isn't __proto__ or something
like it a well defined part of javascript? Shouldn't it be??
A 'super' facility needs read access to the prototype chain. You already
have that with a little extra code in the constructor.

Remember, javascript expects programmers to do more of the work
themselves than do other (compiled) languages.

John
--
John Harris
Oct 31 '07 #6
On Tue, 30 Oct 2007 at 04:25:52, in comp.lang.javascript, Peter Michaux
wrote:

<snip>
>// sex change
cain.__proto__ = eve;
cain.speak(); // NOW like eve
<snip>

How do you arrange that an assignment to cain.__proto__ cuts cain's
salary now that he is a she ?

Rather than change the cain object's type you should create a new cain
belonging to the other type and use that from now on.

John
--
John Harris
Oct 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Dick | last post by:
Derived classes sometimes need to delegate portions of the work in overridden methods to methods in their base classes. This was traditionally done with explicit calls in python, e.g., class...
5
by: Da Costa Gomez | last post by:
Hi, I was wondering whether someone could shed some light on the following. Using inheritance in Java one can override a function f() (or is it overload?) in the child and then do: public f() {...
8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
1
by: cbare | last post by:
Hello JS Gurus, One thing I haven't figured out about javascript is the treatment of __proto__. Inheritence, whether prototypes or class-based, is just a shorthand form of delegation (leaving...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
25
by: dylan m. austin | last post by:
Hello list. This is my first message and it's nothing needful but rather playful. I'd just like to hear some thoughts on something I consider to be a curiosity of javascript usage. I've seen a lot...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.