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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:12:21+00:00 2026-06-12T04:12:21+00:00

I have a Factory class that I use in JavaScript to dynamically load a

  • 0

I have a Factory class that I use in JavaScript to dynamically load a class file over AJAX and then return an object. I have run into a VERY peculiar bug in the system though that throws errors in EVERY browser, but under a condition that is beyond my ability to explain.

Here is a simplified version of my Factory class (I removed a lot of type checking and error handling to cut it down to bare minimum).

function Factory(){
    // This holds which files have already been loaded
    var loaded=new Object();

    // Returns a new object
    this.getObject=function(className,methodName,methodData){

        if(loadFile('class.'+className+'.js')){

            // Making sure that the object name is defined
            if(window[className]!=null){

                // Has to be an object
                if(typeof(window[className])=='function'){

                    // Creating a temporary object
                    return new window[className];
                }
            }
        }
    }
    // Loads a file over AJAX
    var loadFile=function(address){

        // Loads as long as the file has not already been loaded
        if(loaded[address]==null){

            // Required for AJAX connections (without ASYNC)
            var XMLHttp=new XMLHttpRequest();
            XMLHttp.open('GET',address,false);
            XMLHttp.send(null);

            // Result based on response status
            if(XMLHttp.status===200 || XMLHttp.status===304){

                // Getting the contents of the script
                var data=XMLHttp.responseText;

                // Loading the script contents to the browser
                (window.execScript || function(data){
                    window['eval'].call(window,data);
                })(data);

                // makes sure that file is loaded only once
                loaded[address]=true;
            }
        }
    }

This is what the user does:

 var Factory=new Factory();
 var alpha=Factory.getObject('example');
 alpha.set(32);
 alpha.get();
 var beta=Factory.getObject('example');
 beta.set(64);
 alpha.get();
 beta.get();

This fails, it says ‘object is not a function’ when the function is run for the second time (at the return new window[className]; line). I understand if I am missing something here, BUT here’s the kicker:

If I prefix className in my window[] calls, then it works. For example, if I change my 'example' class filename to 'test_example' and then have these lines:

... if(window['test_'+className]!=null){ ...
... if(typeof(window['test_'+className])=='function'){ ...
... return new window['test_'+className]; ...

Then it works and both alpha and beta objects work as expected. When I refer to them purely through a variable, they fail. I tried things like className.toString() without success and even this fails:

className+''

This is really weird, I don’t know where to look and what to try out anymore, does anyone know why this happens?

EDIT: Here is an example of the ‘example.js’ script that is being loaded:

function example(){

    var myVar=16;

    this.set=function(value){
        myVar=value;
    }

    this.get=function(){
        alert(myVar);
    }

}

(and if I rename this to test_example() and load the functions as shown above with constructed strings, then it again works)

  • 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-12T04:12:22+00:00Added an answer on June 12, 2026 at 4:12 am

    I figured out where the error was, which my above, cut-down version does not show. Apparently I named my new variable the same that the name of the class itself was, thus after first initialization it got overwritten.

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

Sidebar

Related Questions

I have a factory class that creates an object based on a parameter it
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have this abstract factory pattern that I use in as3 that I am
I have a class that assists in importing a special type of file, and
I have a static SessionFactory class that initializes an NHibernate session factory. Because this
I have a custom type MyClass and a factory class Factory that creates objects
I have a class that would normally just generate factory objects, however this class
I have a class that I want to put factory methods on to spit
I would like to create a class that describes a file resource and then
I have a factory class that creates objects with circular references. I'd like them

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.