SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Object inheritance

    What would be the difference doing inheritance like this:
    Code:
    function superClass()
    {
    	this.color = "red";
    }
    
    function subClass()
    {
    	this.shape = "circle";
    }
    
    subClass.prototype = new superClass();
    
    test = new subClass();
    alert(test.color); // alerts "red" from superClass
    from this:
    Code:
    function superClass()
    {
    	this.color = "red";
    }
    
    function subClass()
    {
    	this.shape = "circle";
    	superClass.call(this);
    }
    
    test = new subClass();
    alert(test.color); // alerts "red" from superClass
    What would be the benefits using one to the other? Is it really the same thing?

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    protype-based inheritance is dynamic, that is, if you change the proto later in your code, derived object will reflect the changes.


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
  •