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

  • SEARCH
  • Home
  • 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 8353645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:19:24+00:00 2026-06-09T09:19:24+00:00

current broken code: http://jsfiddle.net/9F52n/2/ What I’m trying to do: Learn how to define an

  • 0

current broken code: http://jsfiddle.net/9F52n/2/

What I’m trying to do: Learn how to define an object/function that behaves like a class, and be able to define subclasses, both static , and instantiatable (singleton in my example below).

Currently, my code below doesn’t work all that well. but, if the instantiatable class sand the static class were removed, you’ll see that I have the very basics of class creation down.

So, I guess my question is: what is the proper / most semantic way to define nested clases (singleton or otherwise) with the way I’ve defined TBR? (function(){...})(window)

var TBR = (function() {
    // define local copy of taco bell run
    var TBR = function() {
        return new TBR.fn.init();
    },
        message = "hello world!";

    TBR.fn = TBR.prototype = {
        constructor: TBR,
        init: function() {
            console.log("From TBR Constructor: " + message);
        }
    }

    var InstantiatableClass = function() {
        return new TBR.InstantiatableClass, fn.init();
    }

   InstantiatableClass.fn =InstantiatableClass.prototype = {
        constructor: TBR.InstantiatableClass,
        init: function() {
            console.log("from InstantiatableClass: " + message);
        }
    }

    this.staticClass = function() {
        var subMessage = "little world";
        init = function() {
            console.log("from staticClass: " + subMessage);
        }
    }
    // expose TBR to the window object
    window.TBR = TBR;

})(window);
  • 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-09T09:19:26+00:00Added an answer on June 9, 2026 at 9:19 am
    var InstantiatableClass = function() {
        return new TBR.InstantiatableClass, fn.init();
    }
    
    InstantiatableClass.fn =InstantiatableClass.prototype ...
    

    This does not work. Your InstantiatableClass local variable returns objects, the prototype will not get applied to them. Also, TBR.InstantiatableClass is defined nowhere. If that is what you wanted, you’d need to use

    function InstantiatableClass() {
        // common constructor things
    }
    TBR.InstantiatableClass = InstantiatableClass; // assign a "static" property
    

    Also, you should not [need to] overwrite the prototypes. Sure, the only difference is that constructor is enumerable now (as far as it is not forgotten), but the following would be much cleaner:

    InstantiatableClass.fn = InstantiatableClass.prototype; // shortcut
    InstantiatableClass.fn.init = function() { … };
    

    Oh, you want something that works like jQuery. Imho you should not make the constructor (init) a property of the prototype – that is just very odd and I can’t see a reason to do so. I’d suggest this code:

    window.TBR = (function() {
        function TbrConstructor() {
            …
        }
        function InstantiableConstructor() {
            …
        }
    
        // Now, the creator functions:
        function TBR() { return new TbrConstructor; }
        function Instantiable() { return new InstantiableConstructor; }
    
        // Now, overwrite the "prototype" properties - this is needed for
        // (new TbrConstructor) instanceof TBR === true
        // and also create those fn shortcuts
        TBR.fn = TBR.prototype = TbrConstructor.prototype;
        Instantiable.fn = Instantiable.prototype = InstantiableConstructor.prototype;
    
        // we might overwrite the "constructor" properties like
        TBR.fn.constructor = TBR;
        // but I don't see much sense in that - they also have no semantic value
    
        // At last, make Instantiable a property on the TBR function object:
        TBR.Instantiable = Instantiable;
        // and then only
        return TBR;
    })();
    
    // Usage
    
    TBR(); // returns a TbrConstructor instance
    TBR.Instantiable(); // returns a InstantiableConstructor instance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

current code I've built function to do something over collection of jQuery elements: var
I have an application that is broken into several libraries for purposes of code
My current project is broken down into 3 parts: Website, Desktop Client, and a
i am wondering if the basic link_to syntax is completely broken in current rails3
current code (not working): /^script\s*type=\text\/javascript/i.test(tagName)
Current Application Server Configuration: IIS 6.0 Windows 2003 Server Standard Edition SP2 .NET Framework
On a current project which I'm currently working, i have a symfony task that
I'm working on an ASP.NET MVC solution that has a number of different menus.
lxml's tostring() function seems quite broken when printing only parts of documents. Witness: from
Hey. I was going to make a current class for my menu component, that

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.