Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Does the Prototype JS library add ".prototype" objects as members of a lot of objects? I saw some JS code in the wild and wasn't sure if that meant they were using Prototype (they didn't include prototype.js or anything obvious).
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    The term "prototype" is the Javascript pseudo-equivalent to "class" in Java. It's used to represent the class model, archetype. or "prototype" if you will.

    It's not related to the "Prototype" library... conversely, the library's name is derived from the syntax. Kind of ambiguous if you ask me... it's like me creating a Java library called "Class".

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    The Prototype JS library is named after the prototype property, rather than the other way around, in the sense that it modifies the prototypes of many default objects (such as Element and Array) to add new functions to them.

    You should look into prototype-based OO to see what it means. Basically, OO based on prototypes is based on cloning prototypical objects that contain the properties you want in your actual objects. Typically inheritance is implemented by a prototype `chain', whereby a prototype's prototype is where it delegates things that it can't find itself. There's a lot of research in this area, and there are both older Prototype-based languages (like Self) and newer ones (like Io). It's a pretty neat concept, though very alien to the class-based way of looking at OO.

    Typically the way to define a class in the appropriate way in Javascript is to do something of this type:

    Code:
    var MyClass = new Function();
    MyClass.prototype =
    {
      prop: 'test',
      method: function() {
        return "hello!";
      }
    };
    
    var test = new MyClass();
    test.prop; // => 'test'
    test.method; // => Function object
    test.method(); // => "hello!"
    You make MyClass a function so that it basically becomes the constructor, and then you assign its prototype, which will get cloned into new objects. However, JavaScript uses something called `differential inheritance'. In essence, this means that only differences between the current object and its prototype are kept track of. Above, we can then do:

    Code:
    MyClass.prototype.prop = 'magic!';
    test.prop; // => 'magic!', since test has no prop property and so it is delegated to the prototype
    test.prop = 'w00t';
    MyClass.prototype.prop; // => 'magic!'
    test.prop; // => 'w00t'
    MyClass.prototype.prop = 'nice';
    test.prop // => 'w00t'
    Thus, we can keep updating the default values of the variables until we override the prototype's values with values local to our object.

    To create a subclass of MyClass, we can do the following:

    Code:
    var SubClass = new Function();
    SubClass.prototype = new MyClass();
    SubClass.prototype.newProp = newProp: 'mmmm';
    SubClass.prototype.newFn = function() { return 'go away'; }
    
    sub = new SubClass();
    sub.prop; // => 'nice'
    sub.newFn; // => Function
    sub.newFn(); // => 'go away'
    sub.newProp; // => 'mmm'
    
    SubClass.prototype.prop = 'overridden';
    sub.prop; // => 'overridden'
    In short, you can see a `class' as just an instance of its `superclass', in the Javascript world, where the `starting point', so to speak, for `superclasses' is the Function `class' (or, more accurately, the Function object).

    Thusly, if, in Firebug, you do `Function.prototype', you'll get function(). Now, if you use `real' prototype-based programming, based on regular prototype points of view rather than the `new Class()' syntactic sugar, then you can actually make your `classes' start from the Object `class'. This is because new adds the convenience of having a constructor (which is basically the body of the function that you assign to the class name).

    Anyway, some of this can get tedious, so libraries like prototype wrap it in a nicer syntax. Prototype in particular also provides for mixins where you can mix a set of functions into a class without making it a `superclass' per se (allowing for continuing single inheritance). I don't know how that particular trick is implemented in Prototype, but in Ruby, you get the module with the relevant methods inserted into your inheritance tree; however, when checking what the superclass is (using MyClass.superclass), modules are omitted.

    So, that was more than I intended on writing. In short, prototype-based OO bears investigating. Io is a clean implementation of it, in the sense that it looks like everything is done using pure prototype-based OO constructs. Also, keep in mind that you can always modify prototypes, which is something Prototype takes advantage of to achieve its little trick of adding functions like `each' to all arrays.

    Note also that, in Javascrpit, this can present a problem. According to the ECMAScript standard, the for-in loop iterates over all properties that aren't marked in a certain way (the DontEnum internal property). Since there's no way to set DontEnum on functions that Prototype adds, using a for-in loop to iterate over objects typically breaks when using the Prototype library. Here's some more on DontEnum, including an IE implementation bug.

    Anyway. I'll stop there

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Good writeup... lots of valuable info on prototype-based OO.

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Thanks. It still only brushes the surface, though. Having OO set up that way provides for some very interesting possibilities, and some interesting pitfalls, as well.

  7. #6
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Quote Originally Posted by transio
    The term "prototype" is the Javascript pseudo-equivalent to "class" in Java. It's used to represent the class model, archetype. or "prototype" if you will.

    It's not related to the "Prototype" library... conversely, the library's name is derived from the syntax. Kind of ambiguous if you ask me... it's like me creating a Java library called "Class".
    Yeah, that is a rather confusing way to name a library.
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  8. #7
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    That really cleared it up. Thanks a lot, Shadowfiend, as always.
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:24 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com