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

  • Home
  • SEARCH
  • 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 6363489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:02:42+00:00 2026-05-25T00:02:42+00:00

I have a code as follow in making an object: var myObject = {

  • 0

I have a code as follow in making an object:

var myObject = {
    Init: function() {
        // config anything here to initiate this object.
    };
}

Where myObject.Init() performs like a constructor of a class. I use this Init() to generalize the object to be initiated in a following factory pattern:

window[myObjectName].Init();

myObjectName is whatever name of the object to create.

Then again I have a 2nd choice to create an object, which is much more easier for me to actually turn the Init() into a some sort of a constructor like the code below:

var myObject = function() {
    // config anything here to initiate this object.
}

With the code above, I can simply create an object like the following:

window[myObjectName]();

and practically reduces myObject code on having Init() attached to it (and every other object I would do in the factory).

However the 2nd choice comes with a disadvantage I discovered, where I can’t use this inside the object, that I actually have to explicitly use myObject to call things inside. For example:

var myObject = function() {
    myObject.doSomething(); // instead of this.doSomething();
}

Which concerns me a little on variable/object reference (please correct me if I’m wrong).

Any help on these choices’ advantages and disadvantages? which do you prefer on both of these choices or can you suggest a better solution?

Thanks.

  • 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-25T00:02:42+00:00Added an answer on May 25, 2026 at 12:02 am

    Why not use a constructor function? Both approaches that you suggested requires the object to be created and stored before it can be initialised. You can do like this:

    function MyObject() {
      this.x = 1;
      this.y = 2;
    }
    

    You use the new keyword to create the objects:

    window[myObjectName] = new MyObject();
    

    Another advantage with this is that you can use the prototype of the function to add methods to the objects that are created:

    MyObject.prototype = {
    
      doSomething: function() {
        // do something
      }
    
      doMore: function() {
        // do more
      }
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this filtering entries in an Array of Objects: var
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have code like this to move the player in my game left, right,
I have this code and I want to keep it elegant. I got stuck
I have the follow code - as TreeItemRenderer <?xml version=1.0 encoding=utf-8?> <mx:TreeItemRenderer xmlns:fx=http://ns.adobe.com/mxml/2009 xmlns:s=library://ns.adobe.com/flex/spark
I have this code which builds an IList<string> : IList<string> databases; using (MySqlConnection _conn
I have code that references a web service, and I'd like the address of
I have code to create another row (div with inputs) on a button click.

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.