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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:19:27+00:00 2026-05-26T04:19:27+00:00

Possible Duplicate: Use of .apply() with 'new' operator. Is this possible? I have 5

  • 0

Possible Duplicate:
Use of .apply() with 'new' operator. Is this possible?

I have 5 or 6 variable assignments of the form

var analyteSelection = new TemplatedSelectionContainer($('.analyte-container', this), helpers, optionsTemplate);
var instrumentSelection = new AssetBackedSelection($('.instrument-container', this), helpers, optionsTemplate, Assets.instruments, 'Instrument');
var methodSelection = new AssetBackedSelection($('.method-container', this), helpers, optionsTemplate, Assets.methods, 'Method');

As you can see, a significant amount of portion of these constructors are very much alike. It would be nice if I could create a little generic currying builder that would allow me to do something like:

var newSel = selectionContainerBuilder(this, helpers, optionsTemplate)
var analyteSelection = newSel(TemplatedSelectionContainer, '.analyte-container');
var instrumentSelection = newSel(AssetBackedSelection, '.instrument-container', Assets.instruments, 'Instrument');
var methodSelection = newSel(AssetBackedSelection, '.method-container', Assets.methods, 'Method');

I can achieve something similar with

var selectionContainerBuilder = function(ctx, helpers, optionsTemplate) {
  return function(FuncDef, selector, a, b, c, d, e, f) {
    return new FuncDef($(selector, ctx), helpers, optionsTemplate, a,b,c,d,e,f);
  }
}

But seriously ick. I would like to just be able to splice the first three known parameters to the beginning of the arguments array and apply it to the FuncDef but I’m being foiled by the need to use the new operator.

And before someone asks, I can’t do new-operator enforcement inside of FuncDef because it’s being generated by the coffeescript class keyword.

  • 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-26T04:19:27+00:00Added an answer on May 26, 2026 at 4:19 am

    Of course it can be done. This is the one case where eval turns out to be useful.

    function newApply(cls, args) {
        var argsAsString = [];
        for (var i = 0, l = args.length; i < l; i++) {
            argsAsString.push('args[' + i + ']');
        }
        return eval('new cls(' + argsAsString.join(',') + ')');
    }
    

    (stolen from another thread)

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

Sidebar

Related Questions

Possible Duplicate: Use of var keyword in C# Being relatively new to C# I
Possible Duplicate: Use a variable within a variable? Java Hi this is a general
Possible Duplicate: Use of var keyword in C# Hi, I am pretty new in
Possible Duplicate: Where would I use a bitwise operator in JavaScript? In c/c++ bitwise
Possible Duplicate: Use of var keyword in C# I don't really understand why you
Possible Duplicate: Use of class definitions inside a method in Java Can we have
Possible Duplicate: Use Visual Studio web.config transform for debugging I have an asp.net application
Possible Duplicate: Use Linq to Xml with Xml namespaces I have an xml: <?xml
Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I went to various websites
Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I am confused with these

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.