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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:13:40+00:00 2026-06-07T18:13:40+00:00

I have a javascript module (as we call them in python) in a file

  • 0

I have a javascript “module” (as we call them in python) in a file called thing.js:

function Thing(){
this.a = "foo";
this.b = "bar";
this.c = "";

this.d = function(){
    this.c = this.a + this.b;
};
};

var things = {
'one':Thing(),
'two':Thing(),
'three':Thing()
};
alert(things["one"]); // output: undefined!!??

/* Pick out two random items from things. */
var random_thing = function(){
    //var grabbed = 0;
    //while(grabbed < 1){
var pick = getRandomInt(0,2);
alert(things["one"]); // output: undefined!!
    //if ()
    //return pick;
};

The code is a little incomplete, I want to randomly pick out two items from things and return them. That’s not the immediate issue though.

I have a separate “main” javascript file called main.js, which calls to these objects and functions:

$div1.append(random_thing());

In my html file, I include both javascript files:

<script type="text/javascript" src="thing.js"></script>
<script type="text/javascript" src="main.js"></script>

But the output I keep getting is “undefined” for alert(things[‘one’])! I don’t understand how the first alert returns undefined, it’s right below the definition of the things associative array.

  • 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-07T18:13:42+00:00Added an answer on June 7, 2026 at 6:13 pm

    Calling Thing() will do nothing for you other than mangling the window properties. You’re looking for new Thing()

    var things = {
        'one': new Thing(),
        'two': new Thing(),
        'three': new Thing()
    };
    

    If you call a “class” function without using the new keyword, then this will refer to the window global object, which virtually ensures that things will go wrong – some times terribly so. When you do use the new keyword, this will refer to a brand new object that will be returned automatically.

    This is a common issue with JavaScript “classes” and is (in my opinion) best avoided by using creator functions:

    function Thing() {
        this.a = "foo";
        this.b = "bar";
        this.c = "";
    
        this.d = function(){
            this.c = this.a + this.b;
        };
    };
    Thing.create = function() {
        return new Thing();
    };
    
    var things = {
        'one': Thing.create(),
        'two': Thing.create(),
        'three': Thing.create()
    };
    

    The goal here is to never rely on the new keyword outside of the creator functions.

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

Sidebar

Related Questions

I'm using the javascript module pattern, and have this so far: var APP; if(APP
I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have Javascript function defined in the main index.html file. One part of this
I have javascript code embedded inside a html template file. When I load this
i have a javascript module name SceneModule, see the code snippet below var SceneModule
I have a Javascript module the following Javascript: EntryController = function$entry(args) { MainView(); $('#target').click(function()
I am struggling with this one: I have this simple module in a file
I have JavaScript code which copies the value of input file and paste it
I have javascript string variable with var sttr=We prefer questions that can be answered
I have JavaScript form validation functions like so: function validate_name(field) { if (field ==

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.