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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:16:32+00:00 2026-06-11T12:16:32+00:00

I was hoping I could get some help figuring out why a bit of

  • 0

I was hoping I could get some help figuring out why a bit of my code is causing a stack overflow.

Code in question:

var ClassCreator = {
    create: function(class_object,ParentClass){
        var created_class = null;


        created_class = function(){        
            if(arguments.length == 0){
                this.constructor();
            }else{
                this.constructor.apply(this,arguments);
            }        
        };

        this._grantInheritance(created_class,ParentClass);
        this._grantMethods(created_class,class_object);    

        return created_class;
    },

    _grantInheritance: function(created_class,ParentClass){
        if(ParentClass){
            created_class.prototype = ParentClass.prototype;
            created_class.prototype.BaseClass = ParentClass;
        }
    },

    _grantMethods: function(created_class,creation_object){
        //If there's no constructor provided, add a default constructor.
        if(!creation_object.constructor){
            creation_object.prototype.constructor = function(){};
        }

        //Add the creation_object's methods to the class we're creating.
        for(var property in creation_object){
            created_class.prototype[property] = creation_object[property];
        }
    }
};

var SuperSuperObject = ClassCreator.create({
    constructor: function(){
        document.write("Hello");
    }
});

var SuperObject = ClassCreator.create({
    constructor: function(){
        this.BaseClass.call(this);

        document.write(" ");
    }
},SuperSuperObject);

var RegularObject = ClassCreator.create({
    constructor: function(){
        this.BaseClass.call(this);

        document.write(" World");
    }
},SuperObject);

var test = new RegularObject();​

As far as I can understand, when I call this.BaseClass.call in RegularObjects constructor, it attempts to call RegularObjects constructor again, thus causing the stack overflow. Why it’s calling RegularObject’s constructor and not SuperObject’s constructor, I don’t know. Any ideas?


Edit:
My solution, in case anyone would like it in the future:

var ClassCreator = {
    __PROTOTYPE_CONSTRUCTOR_SIGNAL__:  "1821fe18a870e71b29a6219e076b80bb",

    create: function(class_object,ParentClass){
        var created_class = null;


        created_class = function(){
            var call_class = null;


            if(arguments.length == 1){
                if(arguments[0] == ClassCreator.__PROTOTYPE_CONSTRUCTOR_SIGNAL__){
                    if(this.prototypeConstructor){
                        this.prototypeConstructor();
                    }

                    return;
                }
            }


            if(!this.__construct_stack){
                this.__construct_stack = 0;
            }
            call_class = this;
            for(var counter = 0;counter<this.__construct_stack;counter++){
                call_class = call_class.BaseClass.prototype;
            }
            this.__construct_stack++;


            if(arguments.length == 0){
                call_class.constructor.call(this);
            }else{
                call_class.constructor.apply(this,arguments);
            }


            return this;
        };


        this._grantInheritance(created_class,ParentClass);
        this._grantMethods(created_class,class_object); 

        return created_class;
    },

    _grantInheritance: function(created_class,ParentClass){
        if(ParentClass){
            created_class.prototype = new ParentClass(this.__PROTOTYPE_CONSTRUCTOR_SIGNAL__);
            created_class.prototype.BaseClass = ParentClass;
        }
    },

    _grantMethods: function(created_class,creation_object){
        //If there's no constructor provided, add a default constructor.
        if(!creation_object.constructor){
            creation_object.prototype.constructor = function(){};
        }

        //Add the creation_object's methods to the class we're creating.
        for(var property in creation_object){
            created_class.prototype[property] = creation_object[property];
        }
    }
};
  • 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-11T12:16:34+00:00Added an answer on June 11, 2026 at 12:16 pm

    Problem

    In RegularObject’s constructor, you’re setting the context of its BaseClass method to RegularObject. Now when you enter the SuperObject’s constructor, “this” will be referencing RegularObject(the same object you just came from) and in turn you will be calling RegularObject’s BaseClass method again (making it identical to this.BaseClass.call(this); in RegularObject’s constructor). And because you “call” BaseClass with the same object again, you get a stackoverflow / infinite loop.

    Not the best explanation, but perhaps some examples would help…

    Example

    Here’s a simplified code block that highlight what’s happening

    fiddle: http://jsfiddle.net/GVkDv/1/

    var base = function(){
        //"this" now references the object we just came from along with it's methods 
        //and properties. 
        this.BaseClass.call(this); 
    }
    
    base.prototype.BaseClass = function(){ alert('made it to the base'); }
    
    var derived = function(){
        alert('About to stackoverflow'); 
        this.BaseClass.call(this);//"call" keeps the context to the object we're on 
    }
    
    derived.prototype = new base(); //construct base the first time. 1st Alert.
    derived.prototype.BaseClass = base; 
    
    var x = new derived(); ​
    

    Solution

    To fix it, you’ll need to maintain a context object to reference the instance of the inherited base class.

    Example:

    fiddle: http://jsfiddle.net/bboone/GVkDv/6/

    var superbase = function(){
        var ctx = this; //maintain context of the initialized prototype object
    
        this.init = function(){
            alert('superbase'); 
        };
    
        this.init(); 
    }
    
    var base = function(){
        var ctx = this; //maintain context of the initialized prototype object
    
        this.init = function(){
            //ctx and this are referencing different objects
            ctx.BaseClass.init.call(this); 
        };
    
        this.init(); 
    }
    
    base.prototype = new superbase(); //construct superbase the first time. 1st Alert.
    base.prototype.BaseClass = base.prototype;    
    
    var derived = function(){
        var ctx = this; 
    
        this.init = function(){
            //ctx and this are referencing different objects
            ctx.BaseClass.init.call(this); 
        };
    
        this.init(); 
    }
    
    derived.prototype = new base(); 
    derived.prototype.BaseClass = derived.prototype; 
    
    var x = new derived(); 
    x.init(); //call all the way down the inheritance chain. 
    

    I should point out there are plenty of well documented/vetted inheritance patterns.

    Some examples:

    • John Resig – Simple JavaScript Inheritance
    • Douglas Crockford – Classical Inheritance in JavaScript
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was hoping I could get some help from you guys. The code below
I'm struggling on some coding and was hoping someone could help me out please,
Morning All, I was hoping someone could help or provide some sample code for
I was hoping I could get some help with a Query I am trying
I was hoping I could get some help on the following problem. Basically, I
I was hoping I could get some help optimizing the following data retrieval. Here
Was hoping I could get a bit of a hand on this login that
was hoping you could help me out. I am trying to check if a
Hey guys was hoping you could help me out. basically i'm working on a
hey guys was hoping you could help me out, Not sure if I always

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.