Def : Ensure a class has only one instance and provide a global point of access to it.
Example
var shoppingCart = (function(){
var items= [];
function createinstance(){
return {
getItems : function(){
return items;
},
addItems : function(item){
items.push(item);
}
}
}
return {
GetInstance : function(){
return createinstance();
}
}
})();
Adding items to Shopping Cart
shoppingCart.GetInstance().addItems("one");
shoppingCart.GetInstance().addItems("two");
Get the items from Shopping Cart
console.log(shoppingCart.GetInstance().getItems());
Top comments (0)