String Object - Prototype : Property
Table of content
String Object : Prototype Property Definition and Usage
- The prototype property allows you to add properties and methods to an object.
- Prototype is a global property.
- Prototype is available with almost all JavaScript objects.
Script :
<html> <body> <script type="text/javascript"> function person(name,age) { this.name=name; this.age=age; } var p1=new person("John",21); person.prototype.gender=null; p1.gender = "male"; document.write( p1.name + " is " + p1.gender); </script> </body> </html>
Output :
John is male
Explanation :
var p1=new person("John",21);
- Above Statement will create Person object and we are passing two Constructor Parameters.
- Thus our Person object will have two properties - name and age
function person(name,age) { this.name=name; this.age=age; }
- Now we are going to create our Third Property using Prototype Property of String Object.
Creating Property and Assigning Value
person.prototype.gender=null; p1.gender = "male";