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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:22:51+00:00 2026-05-13T07:22:51+00:00

thanks to wonderful responses to this question I understand how to call javascript functions

  • 0

thanks to wonderful responses to this question I understand how to call javascript functions with varargs.

now I’m looking to use apply with a constructor

I found some interesting information on this post.

but my code is throwing errors

attempt 1:

var mid_parser = new Parser.apply(null, mid_patterns);

error:

TypeError: Function.prototype.apply called on incompatible [object Object]

attempt 2:
attempt 1:

var mid_parser = new Parser.prototype.apply(null, mid_patterns);

error:

TypeError: Function.prototype.apply called on incompatible [object Object]

attempt 2:

function Parser()
{
    this.comparemanager = new CompareManager(arguments);
}

mid_patterns = [objA,objB,objC]
var mid_parser = new Parser();
Parser.constructor.apply(mid_parser, mid_patterns);

error:

syntax_model.js:91: SyntaxError: malformed formal parameter

attempt 3:

var mid_parser = Parser.apply(null, mid_patterns);

error :

TypeError: this.init is undefined // init is a function of Parser.prototype

I have a workaround for the time being:

function Parser()
{
    if(arguments.length) this.init.call(this,arguments); // call init only if arguments
}
Parser.prototype = {
   //...
   init: function()
   {
         this.comparemanager = new CompareManager(arguments);
   }
   //...
}

var normal parser = new Parser(objA,objB,objC);

mid_patterns = [objA,objB,objC]
var dyn_parser = new Parser();
dyn_parser.init.apply(dyn_parser, mid_patterns);

this works pretty well, but it’s not as clean and universal as I’d like.

is it possible in javascript to call a constructor with varargs?

  • 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-13T07:22:51+00:00Added an answer on May 13, 2026 at 7:22 am

    You could use apply and pass an empty object as the this argument:

    var mid_parser = {};
    Parser.apply(mid_parser, mid_patterns);
    

    But that solution will not take care about the prototype chain.

    You could create a Parser object, using the new operator, but without passing arguments, and then use apply to re-run the constructor function:

    var mid_parser = new Parser();
    Parser.apply(mid_parser, mid_patterns);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having to recompile Sphinx ( thanks to this issue – if you can
After a week of posting at the OpenLayers forum and not receiving responses to
I have a wonderful opportunity to refactor out old and smelly code into a
I put together a download script after some wonderful help from stack overflow the
I'm getting into MVC and is wonderful, however, still need to decide what dabatase
Is there a way to make my input buttons prettier? They looked wonderful on
I am trying to get my hands on learning Visual force. I have an
i am working on a problem at the moment that I am having a
I have been trying to create redirects for /home and /index.php to the site's
The corporate guidlines at my company stipulate that our product names are written in

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.