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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:00:01+00:00 2026-05-12T08:00:01+00:00

The problem is that I need to create a new instance of the passed

  • 0

The problem is that I need to create a new instance of the passed class

Is there a way to rewrite this function, so it could accept any number of arguments?

function createInstance(ofClass, arg1, arg2, arg3, ..., argN){
  return new ofClass(arg1, arg2, arg3, ..., argN);
}

This function should create an instance of the passed class. Example:

var SomeClass = function(arg1, arg2, arg3){
   this.someAttr = arg3;
   .....
}
SomeClass.prototype.method = function(){}

var instance = createInstance(SomeClass, 'arg1', 'arg2', 'arg3'); 

So this should be true.

instance instanceof SomeClass == true  

Right now, I’ve just limited N to 25, with hope that more arguments are rarely used.

  • 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-12T08:00:02+00:00Added an answer on May 12, 2026 at 8:00 am

    The other answers are on the right track, but none of them mention that you have to be aware of the fact that arguments is not an Array. It’s a special structure that behaves like an Array.

    So before you use it like an Array, you can convert it to one like this:

    function createInstance(cls) {
        // This will use the slice function of the Array type, effectively converting
        // the arguments structure to an Array and throwing away the first argument,
        // which is cls.
        var args = Array.prototype.slice.call(arguments, 1);
        return cls.apply(this, args);
    }
    

    Sorry, I just copied the code with constructor etc. and didn’t think about what it would actually do. I’ve updated it now to what you want. You’ll find that it’s calling the constructor without new, so you won’t get the same behavior. However, John Resig (author of jQuery) wrote on this very issue.

    So based on John Resig’s article you’ve got two ways to solve it. The more elaborate solution will be the most transparent to the user, but it’s up to you which solution you choose.


    Here is a “perfect” solution if you only intend to support browsers that have the Object.create function (which is a pretty big percentage now compared to three years ago):

    function createInstance(cls) {
        var obj = Object.create(cls.prototype);
    
        var args = Array.prototype.slice.call(arguments, 1);
        cls.apply(obj, args);
    
        return obj;
    }
    

    The resulting objects from both new cls(x) and createInstance(cls, x) should be identical.

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

Sidebar

Related Questions

No related questions found

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.