Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

hold an private array with module pattern js

I’m using module pattern on JS. Trying to make 2 private properties which first property will hold the number i.e 3. The second property is array of string which hold the items.
I’ve been trying but it gives me an error (undefined)

//MODULE DESIGN PATTERN
var myModule = function() {
  //PRIVATE PROPERTIES AND METHODS
  var holdNumber = 3;
  var arrhold = [];

  //private
  function PrivateMessage(obj) {
    console.log("Object successfully added: " + obj)
  }

  //public
  function addObject(obj) {
    arrhold.push(obj);
    PrivateMessage(obj);
  }

  return {
    addItem: addObject
  }
}();

console.log(myModule.addItem("A"));
console.log(myModule.addItem("A"));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

addObject does not return any data from arrhold. If you want to add and print that array, you should add return to addObject

//MODULE DESIGN PATTERN
var myModule = function() {
  //PRIVATE PROPERTIES AND METHODS
  var holdNumber = 3;
  var arrhold = [];

  //private
  function PrivateMessage(obj) {
    console.log("Object successfully added: " + obj)
  }

  //public
  function addObject(obj) {
    arrhold.push(obj);
    PrivateMessage(obj);
    return arrhold; //return arrhold
  }

  return {
    addItem: addObject
  }
}();

console.log(myModule.addItem("A"));
console.log(myModule.addItem("A"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading