Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

array

const collection = {
    length: 0,
    addElements: function(...elements) {
        // obj.length will be incremented automatically
        // every time an element is added.
        
        // Returning what push returns; that is
        // the new value of length property.
        return [].push.call(this, ...elements);
    },
    removeElement: function() {
        // obj.length will be decremented automatically
        // every time an element is removed.
        
        // Returning what pop returns; that is
        // the removed element.
        return [].pop.call(this);
    }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #array
ADD COMMENT
Topic
Name
6+8 =