JavaScript Array prototype
Example
Create a method that transforms array values into upper case:
Array.prototype.myUcase = function() {
for (let i = 0; i < this.length; i++) {
this[i] = this[i].toUpperCase();
}
};
Use the method on any array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.myUcase();
Try it Yourself »
Description
prototype
allows you to add new properties and methods to arrays.
prototype
is a property available with all JavaScript objects.
Syntax
Array.prototype.name = value
Warning
You are not advised to change the prototype of an object that you do not control.
You should not change the prototype of built in JavaScript datatypes like:
- Numbers
- Strings
- Arrays
- Dates
- Booleans
- Function
- Objects
Only change the prototype of your own objects.
The prototype Property
The JavaScript prototype
property allows you to add new properties to objects:
Example
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
Try it Yourself »
Array Tutorials:
Browser Support
prototype
is an ECMAScript1 (JavaScriopt 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |