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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:08:37+00:00 2026-06-17T12:08:37+00:00

I have an object that looks something like this: var myObj = { _fooCon:

  • 0

I have an object that looks something like this:

var myObj = {
  _fooCon: function(f) {
             if (typeof f != 'function') throw new TypeError('Bad Foo');
             this.fn = f;
           },

  _barCon: function(f) {
             if (typeof f != 'function') throw new TypeError('Bad Bar');
             this.fn = f;
           },

  someFoo: function(v) {
             return new this._fooCon(function(x) {
               return x + ' Foo ' + v;
             });
           },

  someBar: function(v) {
             return new this._barCon(function(x) {
               return x + ' Bar ' + v;
             });
           }
};

The reason for doing this is so I can use instanceof productively (i.e., so I can distinguish between the two objects, which are used in different scenarios, despite being structurally identical). (Ignore the fact that someFoo and someBar are similar!)

Is there a way I can abstract the constructor functions, so if I need to create, say, _bazCon, I don’t need to repeat myself; or, if there’s a bug, I don’t have to fix every constructor definition?

I tried making a factory member, like this:

_factory: function(type, f) {
            if (typeof f != 'function') throw new TypeError('Bad ' + type);
            this.fn = f;
          }

…then:

_fooCon: function(f) { return new this._factory('Foo', f); }

Even without trying this, I can see it’s not going to work! Any ideas on how to achieve what I’m looking for?

  • 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-17T12:08:39+00:00Added an answer on June 17, 2026 at 12:08 pm

    If your functions really do the same thing, then it’s as simple as this:

    var myObj = {
      _factory: function(err) {
                    return function(f) {
                        if (typeof f != 'function') throw new TypeError('Bad ' + err);
                        this.fn = f;
                    };
                },
      someFoo: function(v) {
                 return new this._fooCon(function(x) {
                   return x + ' Foo ' + v;
                 });
               },
    
      someBar: function(v) {
                 return new this._barCon(function(x) {
                   return x + ' Bar ' + v;
                 });
               }
    };
    
    myObj._fooCon = myObj._factory("Foo");
    myObj._barCon = myObj._factory("Bar");
    

    If there’s other behavior that differentiates them, then you could have _factory receive a function argument that is invoked within the constructor. That function could use .call or .apply to set the this value to the object being constructed.


    A different approach would be to use a constructor to create myObj, and take advantage of the variable scope so that you don’t need to expose the _xxxCon constructors.

    This uses an anonymous function as a constructor, since we don’t need it again.

    var myObj = new function() {
        var _factory = function(err) {
                    return function(f) {
                        if (typeof f != 'function') throw new TypeError('Bad ' + err);
                        this.fn = f;
                    };
                };
    
        var _fooCon = _factory("Foo");
        var _barCon = _factory("Bar");
    
        this.someFoo = function(v) {
                 return new _fooCon(function(x) {
                   return x + ' Foo ' + v;
                 });
               },
    
        this.someBar = function(v) {
                 return new _barCon(function(x) {
                   return x + ' Bar ' + v;
                 });
               }
    };
    

    You don’t necessarily need the outer function to be used as a constructor, but you do need a function that returns an object to myObj.

    If you did want to expose the _xxxCon functions, then change var to this., and put the this. back in someFoo and someBar.

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

Sidebar

Related Questions

I have a JavaScript object that looks something like this: var myTextOptions = {
I have an object structure that looks like this: var Results = new List<ResultObj>()
I have a json object that looks something like this: Array ( [algorithm] =>
I have a JSON that looks something like this: var countries = [ {
I have a function on my service that looks like something this: addStatement: function(user,
Let's say that I have a object that looks like this var object =
I have an object that looks like this: var obj = { a: text,
I have a pretty simple structure that looks something like this: var list =
I'm using ExtJs 4. I have a panel that looks something like this: var
I have an object that looks like this { AF : { name :

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.