Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript namespace with Singleton usage? I'm stumped!
    primarykey
    data
    text
    <p>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.</p> <p>I saw a pretty good post here that actually was close to what I want (<a href="https://stackoverflow.com/questions/1479319/simplest-cleanest-way-to-implement-singleton-in-javascript">post here</a>), but unfortunately I need to create multiple instances of an Object that is within a namespace, which that example doesn't help with.</p> <p>Here's what I am attempting to do:</p> <pre><code>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(); </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload