JavaScript String prototype
Example
Use the prototype property to add a new property to all objects of a given type:
function employee(name, jobtitle, born)
{
this.name = name;
this.jobtitle = jobtitle;
this.born = born;
}
employee.prototype.salary = 2000;
const fred = new employee("Fred Flintstone", "Caveman", 1970);
Try it Yourself »
Description
The prototype
is a property available with all JavaScript objects.
The prototype
property allows you to add new properties and methods to strings.
Syntax
object.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 »
Browser Support
prototype
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |