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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:50:58+00:00 2026-06-14T00:50:58+00:00

Lets define a dog: function Dog() {} Now lets define a self executing function

  • 0

Lets define a dog:

function Dog() {}

Now lets define a self executing function that passes the Dog as a parameter:

(function(dogClassName) {

    var someServerSideLogicResolvesClassName = "dogClassName";
    //Iniate class name from the server side string
})(Dog);​

After some googleing I found that I should use

new window["dogClassName"]();

or

new this["dogClassName"]();

But since it’s a scoped variable it doesn’t exist in the global name space (no window) and this is undefined; I have no idea how to achieve this without using some sort of evaling.

  • 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-14T00:51:00+00:00Added an answer on June 14, 2026 at 12:51 am

    Right, assuming that you’re trying to access a constructor that isn’t set globally, then the answer to your question is, quite simply: you can’t do this. The scopes of closures are managed differently by the various engines. V8 might even GC the constructor if the returned function doesn’t reference it explicitly…
    There is no magic way to get a full outer scope as an object

    The only solution would be to create a namespace-object:

    (function()
    {
        var constructors = {Constructor1: function(){},
                            Constructor2: function(){}};
        return function()
        {
            var className =getClassNameViaAjax();
            if (constructors.hasOwnProperty(className))
            {
                return new constructors[className]();
            }
            if (window[className] instanceof Function)
            {//fallback
                return new window[className]()
            }
            throw className + ' Does not exist in closure, nor in global scope';
        };
    }());
    

    Have a look at this question, it seems to me to be related, or even a duplicate.

    Hesitant update, but for completeness
    There is one way you can use a variable’s value as reference in the scope chain, but it does require the most Evil of Evil functions: eval('new '+ className + '()'), as you can see in the specification, eval will preserve the this bindings, and reinitialize the scope chains. Except when you’re using 'strict mode';
    Since you’re getting the constructor name from an ajax response, and eval is an easy thing to exploit, don’t do this! This is a sort of an “in theory, you could…” thing.

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

Sidebar

Related Questions

Lets say I define class with method like this: class Test { public function
Is there a way to define a function in PHP that lets you define
I have a web app that lets a client define a daily rate for
Let's say that I define a ClassA , that implements NSCoding protocol (this class
I have something like that (simplified) class A { public: virtual void Function ()
I'm looking to define a method that lets me pass options; something like: @user.tasks(:completed
Can someone please explain this behavior to me. Lets declare a class: Ext.define('baseClass',{ a:null,
I am making an app that lets user define their own data source (SQL),
Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print
The PropertySheet API lets you define a PropSheetProc that can (on Windows XP and

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.