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 8175071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:38:06+00:00 2026-06-06T22:38:06+00:00

I have this base type: typeA = function () { }; typeA.prototype = {

  • 0

I have this base type:

typeA = function () {

};

typeA.prototype = {
  do = function() { alert ("do something"); },
  doMore = function() { this.do(); }
}

and an inherited type typeB:

typeB = function () {

};
typeB .prototype = new typeA();
typeB.prototype.do = function() { alert ("do something else"); };

When I create an instance of typeB and call doMore, I get an error indicating that this.do is not a function. Can I do this sort of thing in Javascript?

  • 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-06-06T22:38:08+00:00Added an answer on June 6, 2026 at 10:38 pm

    Is this example what you are looking for?

    typeA = function () { };
    
    typeA.prototype = {
      do : function() { alert ("do something"); }, //use : instead of = here
      doMore : function() { this.do(); }
    }
    
    typeB = function () { };
    
    typeB.prototype = new typeA();
    typeB.prototype.do = function() { alert ("do something else"); };
    
    var instance = new typeB();
    instance.doMore();
    

    You use : when declaring the properties of an object and = when assigning values to variables. 😀

    Additional explanation:

    This is where the interesting stuff happens:

    typeB.prototype = new typeA();

    When you access a function or variable of an object with ., the browser first looks in the object itself to see if that variable is defined there. This is why you can do things like this:

    var foo = function() {};
    foo.prototype.bar = 3
    
    instance = new foo();
    alert( instance.bar ); //alerts 3
    instance["bar"] = 55;  //add a variable to the instance object itself
    alert( instance.bar ); //alerts 55, instance variable masks prototype variable
    

    This shows how there are two ways that something can be ‘in’ an object. It can either be in the object itself (which you can also do by adding this.bar = 55 to the constructor) or it can in the object’s prototype.

    Hence, when you say typeB.prototype = new typeA(); you are putting everything in that instance of typeA into typeB'prototype. What you’ve basically said is “Hey browser, if you can’t find something in an instance of typeB, look to see if its in this instance of typeA!”

    Turns out there’s nothing actually in that instance, just things in its prototype that end up getting used when the browser can’t find a variable of that name in that object itself. When you call instance.doMore(), the browser can’t find it in instance, so it looks in typeB.prototype, which you just set to an instance of typeA. Since it can’t find anything called doMore in that instance, it looks in its prototype, and finally finds a definition for doMore and happily calls it.

    One interesting thing is that you can still mess around with things that are actually in that instance of typeA that you set to be the prototype:

    //earlier code the same
    
    foo = new typeA();
    typeB.prototype = foo;
    foo.do = function() { alert ("do something else"); };
    //^^ same as `typeB.prototype.do = function() { alert ("do something else"); };`
    
    var instance = new typeB();
    instance.doMore();
    

    While this is kind of cool when you understand what’s going on IMHO, the extra layer of indirection (checking to see if stuff is defined in the instance of typeA before looking in typeA.prototype) is probably not the best idea, and your code would probably be clearer if you just said this:

    typeB.prototype = typeA.prototype;
    

    (sorry if you already knew everything I just told you, but I thought I’d describe how things were working under the hood 😉

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

Sidebar

Related Questions

if i have: function Base (){ this.sayHi = function(){ alert('hi'); } } function Thing
I have this function that embeds flash : function embedswfile(target, swf, base, width, height)
I have a base view V : V = Backbone.View.extend({ initialize: function() { console.log(this.options.z);
I have a base class which has a nested type, inside. There's a function
I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class
I have this jquery script which suppose to hide/show div element base on the
I have this program that should execute a piece of code base on the
In CodeIgniter I'm using a base CRUD My_model , but I have this small
I have a class that inherits from a base class (this contains a lot
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id

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.