Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What does "prototype" do ?

    as in

    String.prototype.functionName = functionName;

    and similair

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    You extend constructors that way.

    Example:

    var myStr = "hello world!";

    String.prototype.reverse = function() {
    return this.split('').reverse().join('');
    }

    alert(myStr.reverse());

    Basically, an object will inherit all methods and properties in its constructor's prototype. Even if the method is added after the object has been initialized, it still gets it,

    That's why Mozilla rocks, you can do stuff like:

    HTMLDivElement.prototype.bla = function() {
    // do something?
    }

    And now you can do document.getElementById('someDivID').bla()


  • #3
    New Coder
    Join Date
    Feb 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In Actionscript (nearly like Javascript), I did extensive research on this question. One thing that's true there: you should avoid using prototype unless it is necessary, as it eats up memory resources, or something like that. The gurus told me.

    I do not know enough yet about Javascript to imagine a case where it is necessary, though the need is obvious enough in Flash, where a target path to a function cannot always easily be established. In that case, it makes sense to use it and they often do - just don't use it for EVERY function.

    Hope that helps.
    Jenn

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I don't see why using prototypes would be inefficient. Every property and method call should look locally, then up the prototype chain until it finds the correct thingy.

    The Javascript 1.5 description of prototype:
    http://devedge.netscape.com/library/...n.html#1193426

  • #5
    New Coder
    Join Date
    Feb 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As I understood it, in Flash, at least, all properties of prototyped objects are stored in active memory, so if it is not necessary to prototype an object, then they put it into a regular old function. This may not be relevant in javascript, but then again, it might.

    Also it probably only matters when you are doing processor-intensive functions. I've seen pages in Flash that, even with expert coding, still took 10 seconds to load, because the processing was so complex. Took us nearly a year to do that 10 seconds, lol! In that case, these things can be very important.

    : )
    Jenn

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by jkd
    You extend constructors that way. [etc..]
    Damn that's cool! Thanks man - I'll make good use of that

    I can see how the lack of general mozilla useage must drive you insane with frustration at times ...

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post

    What does "constructors" do?

    What does constructors do? I realize you can only understand prototype along with this, but to me, it's like the chicken and the egg. One is mentioned with the other and I need an understanding of both, and I can't learn one at a time, can I?

  • #8
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This part...

    var myStr = "hello world!";

    ... is a String, and String is a constructor. Therefore...

    String.prototype.reverse = function()
    {
    return this.split('').reverse().join('');
    }

    ... would add this particular function to the myStr string automatically. You'd call up the functionality like this...

    myStr.reverse();
    ... or even...
    "Hello World!".reverse();

    You can probably do a search on http://devedge.netscape.com for "constructors" for a list of more of them.

    Make sense?

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, that's cool. I'm going to be using this a lot...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #10
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hidden powers of javascript

    This is a powerful property and I've used it to shorten problems where global arrays and all sorts of nonsense would have had to be used.
    Any object type (core and user-defined) can be done in IE and Netscape 4.x.
    As jkd said, Mozilla prototypes even HTMLElements(and all subsets, basically anything that's an Object) so that contrary to popular belief, Mozilla can emulate nearly anything IE does - outerHTML, whatever - with a prototype set up in the head. And of course it goes beyond IE with getters and setters.

    Anyway, back to standard prototyping, I've even prototyped Number object to shorten a double array sort problem(totally cross-browser, well IE/NN anyway). Basically it's excellent for shortening and simplifying problems and coding . Also, gives objects the power to 'own' a simple property(even a String) and so cuts need for global arrays and all sort of error-prone coding.

  • #11
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    I'm not sure if I get it. It sounds alot like typeof

  • #12
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NO!! typeof is simpler

    typeof is also a little bit unknown, but only serves real purpose if used to test against 'undefined'. That's my opinion.
    Whether it's a string, number or whatever, should be dictated by your coding. That's the Java programmer in me speaking. So, only a :
    if (typeof myVariable=="undefined") is worth a lot in my eyes.
    prototype is much more powerful. OK, you could go throughout your life without ever using it, but if you are in need of some object property definition or even code simplification by defining certain objects to possess some method, then prototype is the way to go.

  • #13
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    Do you mean, you can somehow create custom methods on functions?

  • #14
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Errr.... not 100% sure what you mean. Possibly. Give a small example of what you were thinking of(pseudo-code if necessary).

  • #15
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    I mean, something like

    function puzzle()
    {
    // some prototype junk that I don't understand does in here
    }

    if (puzzle().complete){alert()}


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •