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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:31:41+00:00 2026-05-27T23:31:41+00:00

Let us say there are some widgets: function Widget1(initData) { …. } Widget1.prototype.render =

  • 0

Let us say there are some widgets:

function Widget1(initData) {
        ....
} 
Widget1.prototype.render = function() {
    ....
}


function Widget2(initData) {
    ....
}  
Widget2.prototype.render = function() {
    ....
}

What I need to do is the following:

$.subscribe('/launch', function(widgetName, initData) {
    // create a new object of the widget and then call the render method
});

I dont want to write multiple if-else blocks as the number of widgets may become very large. One option is to use eval(), but I believe that there may be better techniques. I am using JQuery framework, so don’t want to include any other frameworks that may have a specific feature to support this. A pure Javascript solution will be appreciated.

  • 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-27T23:31:41+00:00Added an answer on May 27, 2026 at 11:31 pm

    Yep, there is a way. When you create a function, it’s created as a property of the this object in that context. So if you declare those Widget functions in the global scope, they become properties of the window object. As you probably know, you can access a property both as object.property or object['property']. So, if they are global, you can do something like:

    $.subscribe('/launch', function(widgetName, initData) {
        var widget = new window[widgetName](initData);
    });
    

    EDIT: As T.J. Crowder said, I was horribly wrong. What I said about the function being created as a property of this applies when you’re on the global scope (I want to say “only when you’re on the global scope”, but since I’m not 100% sure I’m gonna leave it as that).

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

Sidebar

Related Questions

Let's say there is a function x() which does some animation If I loop
Let's say there is a function like void SendToTheWorld(const Foo& f); and I need
Let's say there is a file called myfile.txt with the following contents: one two
Let's say there is a graph and some set of functions like: create-node ::
I have some trouble understanding parsing XML structures with SAX. Let's say there is
Let's say there is a library function called get_pack() which returns a Pack object:
Let's say that I have some widgets, and each widget has events. Each event
Is there some way to specify a directory (let's say C:\images) and move every
Let's say there's a.gz, and b.gz. $ gzip_merge a.gz b.gz -output c.gz I'd like
Let's say there is a report to compare charges with adjustments that outputs to

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.