Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6021541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:43:42+00:00 2026-05-23T03:43:42+00:00

I am reading this one JS book that talks about inheritance and I am

  • 0

I am reading this one JS book that talks about inheritance and I am really confused at what each line is doing. Can someone let me know if my understanding is correct or not.

function classA(sColor){
   this.color = sColor;
   this.sayColor = function (){
      alert(this.color);
   };
}

function ClassB(sColor, sName){
   this.newMethod = classA; //assigning a reference to a parent class
   this.newMethod(sColor);  //calling the constructor of the parent class

   //question: so what is 'this' then? from my understanding when we call 'new ClassB('a','b')'
   //we instantiate a new object and that new object becomes the 'this' in this case. Does this
   //line mean we are converting the this to classA?

   delete this.newMethod;   //deleting reference to the CONSTRUCTOR of the parent class

   this.name = sName;
   this.sayName = function(){
      alert(this.name);
   }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:43:43+00:00Added an answer on May 23, 2026 at 3:43 am

    How this works

    function ClassB(sColor, sName){
       this.newMethod = classA; 
       this.newMethod(sColor);  
    
       delete this.newMethod;
    
       this.name = sName;
       this.sayName = function(){
          alert(this.name);
       }
    }
    

    Is a mediocre way of doing this

    function ClassB(sColor, sName){
       classA.call(this, sColor); 
    
       this.name = sName;
       this.sayName = function(){
          alert(this.name);
       }
    }
    
    var f = new ClassB("green", "boy");
    f.sayName(); // boy
    f.sayColor(); // green
    

    Your basically calling the classA constructor with your this object.

    JavaScript does not have classes, it just has objects and functions that manipulate objects. Your ClassA function manipulates this and so does ClassB.

    ClassA is just a function that manipulates an object. In this case it manipulates the context object which is this. All ClassB is doing is saying

    • let ClassA manipulate this
    • add a property called name to this
    • add a method called sayName to this

    Bonus:

    There’s a better way to do OO in JavaScript

    // A is a blueprint for an object with a method sayColor
    var A = (function() {
      var self = Object.create({});
      self.sayColor = function() { 
        alert(this.color);
      };
      return self;
    }());
    
    // B is a blueprint for an object with methods sayColor and sayName
    var B = (function() {
      // clone the methods from A
      var self = Object.create(A);
      self.sayName = function() {
        alert(this.name);
      };
      return self;
    }());
    
    // clone B and set the properties for name and color
    var b = Object.create(B, {
      name: { value: "boy" },
      color: { value: "green" }
    });
    
    /* or use a factory
    
    var bFactory = function(name, color) {
      return Object.create(B, {
        name: { value: name },
        color: { value: boy }
      });
    }
    
    b = bFactory("boy", "green");
    
    */
    
    b.sayName();
    b.sayColor();
    

    Use Object.create which is ES5 so use the ES5-shim to support legacy browsers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading a book about AJAX and the writer said that one should
I'm reading this introductory book on parsing (which is pretty good btw) and one
I was reading about this person's interview at a well-known search company. http://asserttrue.blogspot.com/2009/05/one-of-toughest-job-interview-questions.html He
After posting this question and reading that one I realized that it is very
I'm reading a book, where the author talks about fetching an row + all
I was reading this book. Explaing about @OneToOne unidirectional, the author has taken the
I am reading one book about JUnit now and writer advises nulling resources in
I'm reading the Modern Operating System book. And I'm confused about the Page Size.
hello i am reading this python book and one of the exercises says: Write
I am currently reading a book about ASP.NET and I am a bit confused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.