JavaScript prototype Property
❮ Complete Math Object ReferenceDescription
The prototype property allows you to add properties and methods to an object.
Syntax
object.prototype.name = value
Example
In this example we will show how to use the prototype property to add a property to an object:
function employee(name, jobtitle, born) {
this.name = name;
this.jobtitle = jobtitle;
this.born = born;
}
employee.prototype.salary = 2000;
var fred = new employee("Fred Flintstone", "Caveman", 1970);
Try it Yourself »
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 »
❮ Complete Math Object Reference