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

The Archive Base Latest Questions

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

I just learned this way of initializing a variable in javascript: var MyGlobalVar =

  • 0

I just learned this way of initializing a variable in javascript:

var MyGlobalVar = {
    Property1 : 1,
    Property2 : "Two"
}

Which is pretty neat because is a lot like a static object. But is there any way I can run initialization code inside it, kinda like a static constructor would do?

How about using eval, such as Property1 : eval("[code to run here...]");?

I just don’t want to put any initialization or part of my object’s definition outside the object itself (outside the brackets).

Edit: My initialization code would actually return a filled up array, so Property2 would be this array. But I don’t want to recreate this array every time is needed.

  • 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-27T02:20:31+00:00Added an answer on May 27, 2026 at 2:20 am

    If you want a constructor, why not use a constructor?

    var MyGlobalVar = new function() {
          // run whatever code you want in here
    
          // The new object being constructed is referenced via "this"
        this.Property1 = 1;
        this.Property2 = "Two";
    };
    

    This code uses an anonymous function as the constructor. You can run whatever code you want inside. The new object is referenced by this, and it is returned automatically, so MyGlobalVar will look like:

    {
        Property1: 1,
        Property2: "Two"
    }
    

    Another approach, and one that is perhaps more common than using an anonymous constructor, is to use a module pattern. It is very similar, but it doesn’t use the function as a constructor by calling it with new, and it just returns an object literal.

    The above code would be rewritten like this:

    var MyGlobalVar = (function() {
          // run whatever code you want in here
    
          // The new object is an object literal that we create and return
        var _new_obj = {
            Property1: 1,
            Property2: "Two"
        };
    
        return _new_obj;
    })();
    

    Just like the first example, it gives you an enclosed environment where you can run whatever code you need in creating the values to be referenced by your object.

    The end result is practically identical. The only real difference is that the prototype chain of the object in the first example looks like:

    _new_obj ---> {} ---> Object.prototype ---> null
    

    whereas the prototype chain in the new version looks like:

    _new_obj ---> Object.prototype ---> null
    

    So there’s a slightly shorter chain. It won’t make such a difference that you should base any decision on it.

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

Sidebar

Related Questions

I just learned about list comprehension, which is a great fast way to get
As what I just learned, I can use super() this way: super(class, obj_of_class-or-_subclass_of_class) Code
As I just learned from this question , .NET regexes can access individual matches
I have always written regexes like this <A HREF=([^]*) TARGET=_blank>([^<]*)</A> but I just learned
I just posted this question and learned about <see cref=> , however when i
I just learned something interesting. The add method for the javascript select object in
I just learned (the hard way) that Java Component s can only have one
I just learned about this fine looking syntax Collections.<String>emptyList() to get an empty List
I just learned jQuery over the last two days, so I'm kind of new
Possible Duplicate: Is it an anti-pattern to modify JavaScript's built-in prototypes? I just learned

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.