Array.prototype : prototype : Array JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Array » prototype »

 

Array.prototype










Syntax








Array.prototype.property
    Array.prototype.method



The prototype property allows you to add new properties and methods to the Array object that can be used throughout your code.

The following example assigns a new method to the array object with the prototype property.












<html>
    <script language="JavaScript">
    <!--
    function pop()
    {
      if(this.length != 0)
      {
        var lastElement = this[this.length-1];  
        this.length = this.length-1;            
        return(lastElement);
      }
    }
    //Make the pop() function available to all Array objects
    //This will override the pop() method provided by the Array object in Netscape
    Array.prototype.pop = pop;

    var flavorArray = new Array("A","B","C");
    document.write(flavorArray.join(', '),"<br>");

    var removedElement = flavorArray.pop();
    document.write(removedElement," was removed from the flavor array.<br>");

    document.write("new array: ");
    document.write(flavorArray.join(', '),"<br>");

    -->
    </script>
    </html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Array
» prototype