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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:17:08+00:00 2026-05-16T10:17:08+00:00

This is my first post on StackOverflow. This community has provided me with some

  • 0

This is my first post on StackOverflow. This community has provided me with some great insights into lots of different coding questions over the years. So since I’ve been stuck on this particular task in JS for days, I decided I’d lean on this great community and see what sort of help I could get on my question.

I saw a pretty good post here that actually was close to what I want (post here), but unfortunately I need to create multiple instances of an Object that is within a namespace, which that example doesn’t help with.

Here’s what I am attempting to do:

if (!myNamespace) {
  var myNamespace = {};
}

// Object for my namesapce
myNamespace.Item = function() { 
    return  {

        Initialize: function(title,details) {   
            // setting members of this Object
            this.title = title;   
            this.details = details;

        },

        Display: function() {
            this.Position();
            this.Show();    
        },

        Position: function() {
            // position my item in the DOM          
        },

        Show: function() {
            // show my item in the DOM          
        }
    };
}();    

// another Object for my namesapce
myNamespace.Basket = function()  {  
    return  {       
        Initialize: function(title,details,code) {  
            // setting members of this Object
            this.items = [];   
        },
        Add: function(item) {   
            this.items[items.length] = item;                
        }
    };
}();

var Item = new myNamespace.Item;  // the code fails to create a new instance of this Object
Item.Initialize("New Item Title","New Item Desc.");
Item.Display();

var Item2 = new myNamespace.Item;  // the code fails to create a new instance of this Object
Item2.Initialize("New Item Title2","New Item Desc. 2");
Item2.Display();

I’m fairly sure I am thinking of Singleton vs. Class incorrectly. A nice code example with the correct nesting/structure would help SO much! THANKS IN ADVANCE!

  • 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-16T10:17:09+00:00Added an answer on May 16, 2026 at 10:17 am

    The problem is that myNamespace.Item is not a function, is an object, because you have a function that is immediately executed.

    You can for example, add the methods to the current object:

    myNamespace.Item = function() { 
      this.initialize = function(title,details,code) {  
        // setting members of this Object
        this.title = title;   
        this.details = details;
        this.code = code;
      };
    
      this.display = function() {
        this.Position();
        this.Show();    
      };
    
      this.position = function() {
        // position my item in the DOM          
      };
    
      this.show = function() {
        // show my item in the DOM          
      }
    };
    

    Or use the prototype property of the constructor function, to make the object instances created with the new operator inherit those methods:

    // Object for my namesapce
    myNamespace.Item = function() {
      // constructor logic
    };    
    
    myNamespace.Item.prototype.initialize = function(title,details,code) {  
      // setting members of this Object
      this.title = title;   
      this.details = details;
      this.code = code;
    };
    
    myNamespace.Item.prototype.display = function() {
      this.Position();
      this.Show();    
    };
    
    myNamespace.Item.prototype.position = function() {
      // position my item in the DOM          
    };
    
    myNamespace.Item.prototype.show = function() {
      // show my item in the DOM          
    };
    

    Or a slightly shorter syntax:

    myNamespace.Item = function() { };
    
    myNamespace.Item.prototype = {
      initialize: function(title,details,code) {  
        // setting members of this Object
        this.title = title;   
        this.details = details;
        this.code = code;
      },
      display: function() {
        this.Position();
        this.Show();    
      },
      position: function() {
        // position my item in the DOM          
      },
      show: function() {
        // show my item in the DOM          
      },
      constructor: myNamespace.Item // fix the constructor property
    };
    

    The benefit of using the prototype property is that the methods exist only in the myNamespace.Item.prototype object, while in the first example, each object will have its own function instances which is less memory-efficient.

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

Sidebar

Related Questions

This is my first post on stackoverflow, hello everyone! My first venture into jQuery
This is my first post on StackOverflow, so please be gentle... I have some
This is my first post on stackoverflow, so please excuse me if my question
This is my first post in stackoverflow. I want to improve its google pagerank
this is my first post here on stackoverflow and am very impressed by the
This is my first post to stackoverflow, so bear with me. :) I am
First, I must say that I have read several post about this at StackOverflow
This is my first post on stackoverflow, so feel free let me know if
This is my very first post on StackOverflow. Basically, my app starts by checking
this is my first post here on StackOverflow and I'm quite excited about it

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.