Prototype Object.extend(new Base() | Hash | Hash.prototype) usage:

Discussion in 'Javascript' started by jacobstr@gmail.com, Mar 27, 2007.

  1. Guest

    I've noticed Object.extend used in a few different ways and I'm having
    trouble distinguishing why certain usages apply to a given situation.

    >From 1.5.0 prototype.js


    [1] On line 804 Ajax.Base is defined as follows:

    Ajax.Base = function() {};
    Ajax.Base.prototype = {
    setOptions: function(options) { <...>

    [2] Now later on line 821:

    Ajax.Request = Class.create();
    <...>
    Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
    _complete: false, <...>

    [3] Preceding that on lines 636 and 664 respectively we have:
    Object.extend(Hash, {
    toQueryString: function(obj) {

    Object.extend(Hash.prototype, {
    _each: function(iterator) {
    for (var key in this) { <...>


    [4] on line 664
    var Enumerable = {
    each: function(iterator) {
    <...>
    Object.extend(Enumerable, {
    map: Enumerable.collect,



    [1] Seems to be going about things in a traditional manner. No class
    create(). What exactly is the point of class.create()? It seems highly
    redundant.

    [2] There's the assignment operator and Object.extend. Would
    Object.extend(Ajax.Request.prototype,new Base());
    Object.extend(Ajax.Request.prototype, { ... } );
    do the same thing?

    [3] I'm not sure when / when not to use the prototype keyword ...
    actually I don't know what it's for at all! In this example it does it
    both ways. What's the difference? Similar situation with [4] and the
    illustratory alternative I presented in [2]. Would Ajax.Request
    ( without .prototype) be correct? Or would that be broken somehow? I
    wasted some time experimenting in Firefox with this but Javascript is
    pretty fluffy and things like this tend to slip by ~kind of~ working.

    Clarification would be greatly appreciated.
    , Mar 27, 2007
    #1
    1. Advertising

  2. RobG Guest

    On Mar 27, 12:48 pm, "" <> wrote:
    > I've noticed Object.extend used in a few different ways and I'm having
    > trouble distinguishing why certain usages apply to a given situation.
    >
    > >From 1.5.0 prototype.js


    You're lost :)

    Prototype.js help is on the Ruby on Rails spinoffs group:

    news:rubyonrails-spinoffs
    <URL: http://groups.google.com.au/group/rubyonrails-spinoffs?lnk=li >


    --
    Rob
    RobG, Mar 27, 2007
    #2
    1. Advertising

  3. RobG Guest

    On Mar 27, 12:48 pm, "" <> wrote:
    > I've noticed Object.extend used in a few different ways and I'm having
    > trouble distinguishing why certain usages apply to a given situation.


    In my first reply I refrained from further comment, however on reading
    your post again there are some sections that deserve comment here.

    Firstly, I hope you understand the difference between the Prototype.js
    library that is written in javascript and intended to be used with the
    Ruby on Rails environment, and the javascript prototype property of
    objects.

    It is a very poorly named library.

    > >From 1.5.0 prototype.js


    To help avoid confusion with the javascript prototype property, the
    library is normally referenced as "Prototype.js".

    >
    > [1] On line 804 Ajax.Base is defined as follows:
    >
    > Ajax.Base = function() {};
    > Ajax.Base.prototype = {
    > setOptions: function(options) { <...>
    >
    > [2] Now later on line 821:
    >
    > Ajax.Request = Class.create();
    > <...>
    > Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
    > _complete: false, <...>
    >
    > [3] Preceding that on lines 636 and 664 respectively we have:
    > Object.extend(Hash, {
    > toQueryString: function(obj) {
    >
    > Object.extend(Hash.prototype, {
    > _each: function(iterator) {
    > for (var key in this) { <...>
    >
    > [4] on line 664
    > var Enumerable = {
    > each: function(iterator) {
    > <...>
    > Object.extend(Enumerable, {
    > map: Enumerable.collect,
    >
    > [1] Seems to be going about things in a traditional manner. No class
    > create(). What exactly is the point of class.create()? It seems highly
    > redundant.


    That is definitely for the Prototype.js news group - I'd like to know
    the answer myself. The "official" Prototype.js documentation on
    Class.create is at:

    <URL: http://www.prototypejs.org/api/class >

    and says:



    >
    > [2] There's the assignment operator and Object.extend. Would
    > Object.extend(Ajax.Request.prototype,new Base());
    > Object.extend(Ajax.Request.prototype, { ... } );
    > do the same thing?


    The documentation says:


    > [3] I'm not sure when / when not to use the prototype keyword ...
    > actually I don't know what it's for at all!


    Then best you learn - it is fundamental to the object-oriented aspects
    of the language. Try the following two articles, take your time
    getting through them:

    <URL: http://www.litotes.demon.co.uk/js_info/private_static.html >

    <URL: http://www.jibbering.com/faq/faq_notes/closures.html >


    > In this example it does it
    > both ways. What's the difference? Similar situation with [4] and the
    > illustratory alternative I presented in [2]. Would Ajax.Request
    > ( without .prototype) be correct? Or would that be broken somehow? I
    > wasted some time experimenting in Firefox with this but Javascript is
    > pretty fluffy and things like this tend to slip by ~kind of~ working.


    Javscript, or more strictly ECMAScript Language, is not "fluffy" at
    all. It adheres to a public specification that, while often
    criticised as difficult to read and understand, is pretty solid in
    most respects. I fear that your confusion is caused by using
    Prototype.js and not understanding what or why things are happening -
    there are about 3,000 lines of code to get through.

    While some aspects of the javascript language have been criticised
    (maybe rightly so, I'm certainly not qualified to judge either way),
    its prototype-based inheritance is universally viewed as one of its
    great strengths.

    However, prototype-based inheritance can be difficult for those
    schooled in classic OO programming to come to terms with. My only
    advice there is to stop thinking of javascript in terms of classic OO
    concepts (stuff like "Class.create" when javascript has no classes)
    and approach prototype-based inheritance as a brand new concept.


    > Clarification would be greatly appreciated.


    I'm not sure I've helped, but here's hoping. :)


    --
    Rob
    RobG, Mar 27, 2007
    #3
  4. Guest

    Thank you Rob. I'll read those links regarding the prototype keyword
    (I was aware it had nothing to do with the prototype.js library - I'm
    ignorant but not quite ~that~ ignorant ;). Wasn't aware there was a
    prototype group either so thanks for the heads up!
    , Mar 27, 2007
    #4
    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. Replies:
    10
    Views:
    1,035
    Jochen Schulz
    Jul 9, 2006
  2. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  3. Matt Kruse
    Replies:
    7
    Views:
    151
    Richard Cornford
    Feb 5, 2007
  4. Replies:
    3
    Views:
    115
  5. Kees

    extend prototype chain?

    Kees, Jan 25, 2010, in forum: Javascript
    Replies:
    26
    Views:
    250
    Jorge
    Jan 29, 2010
Loading...

Share This Page