www.webdeveloper.com
Results 1 to 14 of 14

Thread: [RESOLVED] How prototype works?

  1. #1
    Join Date
    May 2008
    Posts
    55

    resolved [RESOLVED] How prototype works?

    I am not quite sure how prototype works in JavaScript. I heard that it is an internal property of a function. Buy it is still an vague concept to me. Can we define it.

    I hope that somebody uses the following example to illustrate it.

    Code:
    function BinarySearchTree() {
        this._root = null;
    }
    
    BinarySearchTree.prototype = {
    
        //restore constructor
        constructor: BinarySearchTree,
    
        add: function (value){
        },
    
        contains: function(value){
        },
    
        remove: function(value){
        },
    
        size: function(){
        },
    
        toArray: function(){
        },
    
        toString: function(){
        }
    
    };
    Can we move add, contains etc out of it to create separate functions?

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,223
    prototype extends, thats all it does.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    May 2008
    Posts
    55
    Thanks, but it is sort of simple .......

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    ...Can we move add, contains etc out of it to create separate functions?
    Code:
    function BinarySearchTree(){}
    
    BinarySearchTree.prototype._root=null;
    BinarySearchTree.prototype.add=function(){bla-bla-bla}
    etc.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    May 2008
    Posts
    55
    I still don't understand its concept and usage.
    Why we don't use the methods directly?
    Such as:
    Code:
    function add(){
    // blah blah
    }
    function remove(){
    // blah blah
    }
    Which means they are independent with function BinarySearchTree()?
    What is the advantage of prototype?

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    there is a good reading (http://ejohn.org/blog/secrets-of-the...inja-released/) which, i hope, contains all the answers and even more
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,223
    prototype as I said extends an existing object, for example you may have some Date.prototype.funcame = function(){ ... } that extends the range of properties and methods of the Date object.

    Same applies to an Object you make with the Object Object
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  8. #8
    Join Date
    May 2008
    Posts
    55
    Okay, http://www.w3schools.com/js/js_lib_prototype.asp also mentions propotype, are they same?

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,223
    Personally, I steer clear of google api's or frameworks, I prefer to write what I need.

    If you want to produce a website with bells and whistles, then knck yourself out, however, you sould be aware of the load you put on a users connection having load a file each page, if your scripting is bespoke to your site and only contains what your site needs, your payload is less, you know what goes in to your page and how to fix things.

    Wit ha framework, you rely on others code and for that code to work as expected, the only good thing I can say about frameworks is that they are designed to be cross browser compatible. That again is something I believe shouldn't happen, W3C set the standard and it is then up to browser developers to tow the line, they don't and prefer instead to go off and do their own thing because they think that they know better when the compliance issues are all created because none of them, microsoft included fails to stick to what W3C have set as the standard.

    In a perfect works, one script would work in all browsers and you wouldn't need to crawl up your own rear to get your site and code working. HTML was originally designed to be readable even if an error occurs on a page, these days that is just not possible with browser manufacturers having hissy fits over what they want to do and not what they are told to do.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  10. #10
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    you wouldn't need to crawl up your own rear - wow, perfect! thanks, Mr.Slashes, that extends my english dictionary!
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  11. #11
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,223
    Glad I could enlighten you Padonak.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  12. #12
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    250
    I see this thread has been marked as RESOLVED but I'm going to write my answer anyway...

    Objects are unordered collection of properties where each of which has a name and a value. Object can hold it's own properties, inherited properties or combination of previous two. In JavaScript every object has another object (or null but this is rare) associated with it from which it inherits properties. In addition to it's properties each object has a prototype attribute.

    Prototype - is a reference to another object from which properties are inherited.

    The value of prototype attribute depends on which method you use to create your object. All objects created by object literals use Object.prototype as it's prototype. Objects created with a constructor function use constructor's function prototype property as their prototype value.

    One thing to note here is that all built-in constructor functions inherits from Object.prototype and Object.prototype is the only object which has it's prototype set to null (which means it doesn't inherit any of properties). So for example arrays created by Array constructor function inherits it's properties from Array.prototype and Object.prototype. This is known as a prototype chain.

    So if you want to have multiple objects which inherits same properties you can do something like this:

    Code:
    function Test() {} //creates dummy function
    
    Test.prototype = {test: "This property is inherited by all instances of the Test constructor function!!!"} //sets prototype
    
    var o = new Test(), o1 = new Test() //creates two, new distinct objects
    console.log(o.test) //prints the value of the test property
    console.log(o1.test) //prints the value of the test property
    Last edited by tech_soul8; 01-24-2014 at 09:17 AM.

  13. #13
    Join Date
    May 2008
    Posts
    55
    @tech_soul8, it is a great help. Thanksssssssss.

  14. #14
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    250
    You're welcome!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles