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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:09:32+00:00 2026-06-01T11:09:32+00:00

If I have a sting such as AppNamespace.SomeObject.ClassName and I know that the string

  • 0

If I have a sting such as "AppNamespace.SomeObject.ClassName" and I know that the string represents and actual constructor function, what is the best/recommend way of getting that to use in a statement like var foo = new AppNamespace.SomeObject.ClassName?

I could do:

var s = "AppNamespace.SomeObject.ClassName"
var foo = new (eval(s))

or something like:

var parts = "AppNamespace.SomeObject.ClassName".split(".")
var foo = new (window[parts[0]][parts[1]][parts[2]])

but I’m wondering if there are better solutions that don’t involve eval or having to split the string and loop through its parts. Does anyone have any ideas? If not, based on the two solutions I’ve proposed, what are the pros and cons of each?

  • 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-01T11:09:35+00:00Added an answer on June 1, 2026 at 11:09 am

    You could do…

    var obj = new ('AppNamespace.SomeObject.ClassName'
                    .split('.')
                    .reduce(function(object, key) {
                                return object[key];
                            }, window));
    

    jsFiddle.

    On older browsers, you could shim reduce().

    This code splits the string by the period (.) and then iterates over each from left to right, starting with window as the start object (change it if it isn’t) and getting each sub property until it arrives with the far right (ClassName in your example).

    The new (...) instantiates this object.

    You could write it as a reusable function…

    var getObjectByString = function(string, baseObject, delimiter) {
        // Only checks for string primitives, but that's OK for this example.
        if (typeof string !== 'string') {
            throw new TypeError('First argument is required and must be a string.');
        }
    
        baseObject = baseObject || window;
        delimiter = delimiter || '.';
    
        return string
                .split(delimiter)
                .reduce(function(object, key) {
                          return object[key];
                        }, baseObject));
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have string separated by dot ('.') characters that represents a hierarchy: string source
I have a string such as 'xxox-x' that I want to mask each line
I have a JSON string such as below. That comes from a Website (the
Say I have a string such as abc123def456 What's the best way to split
I have a string such as big bad dog, how can I get an
If I have a string such as lotsofcrapsomethingimportantmoreotherstuff is it possible to get Regex
Lets say a have a string such as this one: string txt = Lore
I have an input string such as x=y|a=b|c=10 etc which gets converted to dynamic
hey I have a string as such: 357902||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC I wish to take

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.