JavaScript namespace using Singleton? I'm at a dead end!

This is my first post on StackOverflow. This community has given me some insight into many different coding issues over the years. Therefore, since I lingered on this specific task in JS for several days, I decided that I would subscribe to this great community and see what help I could solve on my issue.

I saw a good entry here, which really was close to what I want ( post here ), but, unfortunately, I need to create several instances of an object that in the namespace, with which this example does not help.

Here is what I am trying 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();

, Singleton vs. Class. / ! !

+3
2

, myNamespace.Item , , , .

, , :

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          
  }
};

prototype , , new, :

// 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          
};

:

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
};

prototype , myNamespace.Item.prototype, , .

+2

, . , . 'this'. "" , , 'this' .

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

myNamespace.Item = function() {
    this.x = 10;
};

var item = new myNamespace.Item();
alert(item.x);

. , - :

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

myNamespace.get = function() {
    if (typeof(this.singleton) === 'undefined') {
        var Item = function() {
            this.x = 10;
        };
        this.singleton = new Item();
    }
    return this.singleton;
}

var item = myNamespace.get();
alert(item.x);
+1

Source: https://habr.com/ru/post/1761027/


All Articles