prototype __proto__ super and delegation

Discussion in 'Javascript' started by cbare, Oct 29, 2007.

  1. cbare

    cbare Guest

    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
    cbare, Oct 29, 2007
    #1
    1. Advertising

  2. 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?

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


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


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Oct 29, 2007
    #2
    1. Advertising

  3. cbare

    cbare Guest

    Thanks for the reply.

    AOP = aspect-oriented-programming. Aspects allow you to modularize
    certain types of operations that are otherwise prone to being
    intermingled with other code. A big part of AOP involves wrapping
    method calls so that you can do something before each invokation of
    the method and something afterward.

    The something might be: check the user's permissions, log something,
    start a timer then report how long the method took to execute, or
    begin and then commit a transaction. In my particular case, I wanted
    to implement lazy loading.

    The AOP reference isn't really the important thing. It's just one way
    of describing what I had in mind, which was to create an object that
    wraps another object and layers on some functionality before
    delegating to the wrapped object. Same as the decorator pattern. The
    goal is just to separate the additional functionality (lazy loading)
    from the core object so my walnut-sized brain can concentrate on one
    thing at a time.

    I suppose constructor.prototype is just as good, but I always seem to
    have to set it manually, which is something else I don't get.

    I suspect that moving to dynamic languages involves a shift in
    thinking of the same order as first learning OO. Thanks for the hints,

    -chris
    cbare, Oct 30, 2007
    #3
  4. On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <>
    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
    Peter Michaux, Oct 30, 2007
    #4
  5. Peter Michaux wrote:
    > On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <>
    > 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
    Thomas 'PointedEars' Lahn, Oct 30, 2007
    #5
  6. On Oct 30, 2:52 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > Peter Michaux wrote:
    > > On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <>
    > > 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-oriented_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
    Peter Michaux, Oct 30, 2007
    #6
  7. Peter Michaux wrote:
    > [...] Thomas 'PointedEars' Lahn [...] wrote:
    >> Peter Michaux wrote:
    >>> On Oct 29, 4:05 pm, Thomas 'PointedEars' Lahn <>
    >>> 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-oriented_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>
    Thomas 'PointedEars' Lahn, Oct 30, 2007
    #7
  8. 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
    John G Harris, Oct 31, 2007
    #8
  9. 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
    John G Harris, Oct 31, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,740
    Darryl Pierce
    Feb 24, 2005
  2. Robert Dick
    Replies:
    1
    Views:
    308
    Thomas Guettler
    Dec 17, 2004
  3. Sam Roberts
    Replies:
    4
    Views:
    298
    Sam Roberts
    May 7, 2008
  4. cbare
    Replies:
    1
    Views:
    88
    cbare
    Nov 1, 2007
  5. webEater
    Replies:
    3
    Views:
    108
    Thomas 'PointedEars' Lahn
    Jun 15, 2010
Loading...

Share This Page