SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Prototype VS Inheritance (Basic)

    Hi guys,

    I'm a little confused with these two.
    Okay here is my understanding.

    Prototype - extend an object, just like in php you extend a class.
    Inheritance - reuse existing codes, by copying or by reference.

    Sometimes I'm thinking they are just the same.
    Can someone give more advice.

    Thanks in advance.
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by solidcodes View Post
    Sometimes I'm thinking they are just the same.
    Can someone give more advice.
    When speaking about the prototype, you are focusing on the parent object (or further parents) itself.
    With inheritance, you are focusing instead on the something further down from the parent that inherits from the further up.

    For example:

    Code javascript:
    function Cat() {
     
    }
    Cat.prototype.speak = 'Meow';
     
    this.cat = new Cat();
    this.cat.speak; // Meow
     
    this.snagglepuss = new Cat();
    this.snagglepuss.speak = 'Heavens to Murgatroyd!';
     
    this.snagglepuss.speak // Heavens to Murgatroyd!

    The Cat constructor function has a prototype property called speak. That speak property will be accessible to all objects created from that constructor.
    The cat object inherits the speak property from the Cat constructor.

    If the cat object though defines a different property though, then that will be used instead. Searching only goes up the prototype chain until the first matching property is found.

    Please paste the above code in to the Object Playground, and you can see a fancy diagram of how things are structured.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @paul_wilkins ;

    Thanks dude, I think I need more practice.
    Quality codes are optimized and tested...
    Click here for inspiration..

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,263
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    Prototype VS Inheritance ... Sometimes I'm thinking they are just the same.
    "Inheritance" is an abstract idea or feature. "Prototype" is a concrete implementation of that idea.
    "First make it work. Then make it better."


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
  •