The prototype
can add
new properties and methods to the Array() object.
Array.prototype adds the methods and properties to the Array class, therefore all Array() object will have the added features.
prototype |
Yes | Yes | Yes | Yes | Yes |
Array.prototype.newName = value;
The following code shows how to add a new method to Array class to convert all element to upper case.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
Array.prototype.myUcase = function() {<!-- w w w. ja va 2 s.c o m-->
var i;
for (i = 0; i < this.length; i++) {
this[i] = this[i].toUpperCase();
}
}
function myFunction() {
var array1 = ["a", "b", "c", "d"];
array1.myUcase();
document.getElementById("demo").innerHTML = array1;
}
</script>
</body>
</html>
The code above is rendered as follows: