Creé una variable para contener la cantidad total de elementos en la matriz usando myArray.length. Luego uso myArray.push() para agregar un elemento a la matriz.
la matriz original tiene 3 elementos, por lo que .length devuelve 3, lo cual es correcto. Pero cuando agregué un elemento a la matriz y consola.log (el Var que contiene la propiedad .length), simplemente no funciona. el Var todavía muestra 3 para la matriz. Sin embargo, si uso xxx.length, el número es correcto ahora.
var collection = function() { //two properties var items = ["basketball", "football", "soccer"]; var number = items.length; console.log(items); console.log("the number of the items in the array is " + number); // two private methods var addItem = function(newItem) { items.push(newItem); console.log(items); }; var showNumber = function() { console.log(number) //this one returns 3, which is not correct console.log("the number of the items in the array is " + items.length); }; // this one returns 4, which is correct // two public methods return { getItem: addItem, getNumber: showNumber, }; }(); collection.getItem("pingpong"); collection.getNumber();