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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:25:52+00:00 2026-05-13T06:25:52+00:00

I use Resig’s makeClass() approach for constructors: // makeClass – By John Resig (MIT

  • 0

I use Resig’s makeClass() approach for constructors:

// makeClass - By John Resig (MIT Licensed)
// Allows either new User() or User() to be employed for construction. 
function makeClass(){
  return function(args){
    if ( this instanceof arguments.callee ) {
      if ( typeof this.init == "function" )
          this.init.apply( this, (args && args.callee) ? args : arguments );
    } else
      return new arguments.callee( arguments );
  };
}

// usage:
// ------
// class implementer:
//   var MyType = makeClass();
//   MyType.prototype.init = function(a,b,c) {/* ... */};
// ------
// class user:
//   var instance = new MyType("cats", 17, "September");
//      -or-
//   var instance = MyType("cats", 17, "September");
//



var MyType = makeClass();

MyType.prototype.init = function(a,b,c) {
    say("MyType init: hello");
};

MyType.prototype.Method1 = function() {
    say("MyType.Method1: hello");
};

MyType.prototype.Subtype1 = makeClass();

MyType.prototype.Subtype1.prototype.init = function(name) {
    say("MyType.Subtype1.init:  (" + name + ")");
}

In that code, MyType() is a toplevel type, and MyType.Subtype1 is a nested type.

To use it, I can do:

var x = new MyType(); 
x.Method1();
var y = new x.Subtype1("y"); 

Can I get a reference to the instance of the parent type, within the init() for Subtype1() ?
How?

  • 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-13T06:25:52+00:00Added an answer on May 13, 2026 at 6:25 am

    Nope, not unless you write a class implementation that tracks this “outer” class explicitly, Javascript won’t be able to give this to you.

    For example:

    function Class(def) {
        var rv = function(args) {
            for(var key in def) {
                if(typeof def[key] == "function" && typeof def[key].__isClassDefinition == "boolean")
                    def[key].prototype.outer = this;
                this[key] = def[key];
            }
    
            if(typeof this.init == "function")
                this.init.apply( this, (args && args.callee) ? args : arguments );  
        };
    
        rv.prototype.outer = null;
        rv.__isClassDefinition = true;
        return rv;
    }
    
    var MyType = new Class({
        init: function(a) {
            say("MyType init: " + a);
            say(this.outer);
        },
    
        Method1: function() {
            say("MyType.Method1");
        },
    
        Subtype1: new Class({
            init: function(b) {
                say("Subtype1: " + b);
            },
    
            Method1: function() {
                say("Subtype1.Method1");
                this.outer.Method1();
            }
        })
    });
    
    var m = new MyType("test");
    m.Method1();
    
    var sub = new m.Subtype1("cheese");
    sub.Method1();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use Product/@Id="*" to let WiX generate product codes for upgrades.… May 13, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer How about this: gsub('(.{1,90})(\\s|$)', '\\1\n', s) It will break string… May 13, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer The most popular options are Qt and wxWidgets. I, personally,… May 13, 2026 at 8:53 pm

Related Questions

I don’t think I’ve grokked currying yet. I understand what it does, and how
I am trying to understand the internals of how jquery framework is written and
We recently built a web app using Prototype, making a fair amount of use
Sorry for the semi-rant here. I am hooked on javascript and want some direction
The solution to this question suggested the use of John Resig's class implementation. This

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.