SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: the prototype

  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    the prototype

    i want to learn javascript recently,now,there is one concepts i can't understand,which is prototype, in javascript object, i see it show often,and there is also a javascript framwork named prototype. very confused. hope anyone can give me some more datails on the prototype in javascript object
    Outdoor tools online store and wargame tools shop
    with best quality and service.
    comp molle tactical vest tactical clothing etc sell

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    The library called "Prototype" was probably badly named, like "Javascript" itself.

    The prototypal inheritance is the basis of how one object can automatically have the same properties and/or methods of another object in Javascript, as opposed to the way most other languages do it (they use classes).

    You can also manually call out the method called "prototype".

    Code:
    if (Function.prototype.call) {
        listener.call(target, event);
    }
    Here it's looking specifically for the "call" method of the "prototype" method of the Function object. That's one of the default, built-in objects in Javascript:
    Function
    Array
    Date
    RegExp
    String
    Number
    ... (there are more)

    These are already sitting in Javascript and they are pre-built with their own special methods (functions that are special to them). Technically these all belong ultimately to their "prototype" property, and I'm still new enough not to understand when you need to specifically mention "prototype" by name like that.

    But, for example, the Array object has a method called "length". All Arrays have a .length method, and any time you make a new Array, it's already got that, as part of the Array object's prototype. All new Arrays you make, they are inheriting that method from the main Array object, no matter how you actually make your own array.

    using new
    var foo = new Array(10);

    You can now call foo.length, without having to manually add in "length" to foo... it came with foo being an array.

    using an array literal:
    var foo = [];

    Even though it's empty at this point, it's still an array. If you asked what TypeOf it was, it would be an Array object. So it has a "length" method by default.

    When you make a function
    var foo = function () {
    };
    or
    foo: function (){
    }
    or
    var foo = new Function(){}; (not recommended)
    or
    function foo() {
    }

    you get the stuff that Function objects have, like "call" and "args".

    Does this make sense?

  3. #3
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    using new
    var foo = new Array(10);

    using an array literal:
    var foo = [];

    in the above,how to use an array literal declare to make the foo.length equal to 10.
    Outdoor tools online store and wargame tools shop
    with best quality and service.
    comp molle tactical vest tactical clothing etc sell

  4. #4
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    why in "var foo = new Array(10);
    foo.length;" can't write like this
    foo.prototype.length;
    Outdoor tools online store and wargame tools shop
    with best quality and service.
    comp molle tactical vest tactical clothing etc sell

  5. #5
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    if (Function.prototype.call) {
    listener.call(target, event);
    }
    i am sorry,i don't understand this code.
    Outdoor tools online store and wargame tools shop
    with best quality and service.
    comp molle tactical vest tactical clothing etc sell

  6. #6
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    in the above,how to use an array literal declare to make the foo.length equal to 10.
    I forget, cause usually I start with [] and just fill it with whatever I need to fill it with.
    You can prolly declare it:

    var foo=[];
    foo.length = 10;


    Ah, this page describes "prototype" for arrays:
    http://www.javascriptkit.com/jsref/arrays.shtml

    why in "var foo = new Array(10);
    foo.length;" can't write like this
    foo.prototype.length;
    Quote Originally Posted by JavascriptKit
    Use this property to attach additional properties and/or methods that get reflected in all instances of the array.
    From the link above. So, you can't say foo.prototype.length, but let's say you made some arrays and none of them are based on each other, but you want all of them to have a new property, you could

    var cat=[], dog=[], moose=[];
    so clearly all arrays. You want to add a "fur" property to all arrays in your script, because you know they're all going to be hairy animals:

    Array.prototype.fur = (something);

    now there's cat.fur, dog.fur, and moose.fur. Plus there's a .fur for any new arrays you make (if I have this correctly).


    Code:
    if (Function.prototype.call) {
    listener.call(target, event);
    }
    i am sorry,i don't understand this code.
    I don't either, I grabbed it from SitePoint's Core library which you can use to make sure IE works the same as other browsers regarding "this" and event loaders/listeners. I saw that in the code; it SEEMS to be checking IF the Function base object contains a prototype with "call" in it (maybe checking for an old version of IE that doesn't have .call?? I dunno).

    Another one I've seen was Mozilla's code to make other browsers add indexOf to Arrays:
    https://developer.mozilla.org/En/Cor.../Array/IndexOf
    Under "compatibility" they have code, which I've copied too, that adds a method "indexOf" to Array objects.

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  8. #8
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Nice!

    a prawn and mayo sandwich
    What are we gonna do about them fookin prawns, man? I lol'd.


Bookmarks

Posting Permissions

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