JavaScript Object Prototypes

No comments

              JavaScript Object Prototypes

Every JavaScript object has a prototype. The prototype is also an object.
All JavaScript objects inherit their properties and methods from their prototypes.

JavaScript Prototypes:
      Objects created using an object literal, or with new object(), inherit from a prototype  called object.prototype.
     Objects created with new Date() inherit the Date.prototype.
The Object.prototype is on the top of the prototype chain.
All JavaScript objects (Date, Array, RegExp, Function, ... .) inherit from the object.prototype.

JavaScript Object Prototypes
Creating a Prototype:

      The standard way to create an object prototype is to use an object construction function.

Example:
         function Person(first, last, age, eyecolor){
            this.firstName=first;
            this.lastName=last;
            this.age =age;
            this.eyecolor=eyecolor;
          }
with a construction function, you can use the new keyword to create new object from the same.

Example:
       var myFather= new Person("John", "Deo", 50, "black");
      var myMother= new Person("Sally", "Rolly", 42,"green");

<!DOCTYPE html>
<html>
  <body>
      <p id="demo"> </p>
     <script>
     function Person(first, last, age, eye){
               this.firstName=first;
               this.lastName=last;
               this.age=age;
              this.eyecolor=eye;
}
var myFather=new Person("John", "Deo", 50, "black");
var myMother=new Person("Sally", "Rolly", 42, "green");
document.getElementById("demo").innerHTML="my father is"+myFather.age+"my Mother is"+myMother.age;
</script>
</body>
</html>

The construction function is the prototype for Person objects.
It is considered good practice to name constructor function with an upper case first letter.

Adding Prototypes and Methods to objects:

*Sometimes we want to add new properties (or methods) to an existing object.
*sometimes we want to add new properties (or methods) to all existing objects of a given type.
*Sometimes we want to add properties (or methods) to an object prototype.

Adding a property to an object:

Adding a new property to an existing object is easy.

myFather.nationality= "English";

<!DOCTYPE html>
<html
 <p id="demo"></p>
<script>
function Person(first, last, age, eye) {
this.firstName=first;
this.lastName=last;
this.age=age;
this.eyecolor=eye;
}

var myFather=new Person("John", "Deo", 50, "blue");
var myMother=new Person("Sally","Rolly", 48, "green");
myFather.nationality="English";
document.getElementById("demo").innerHTML="myFather is "+ myFather.nationality;
</script>
</body>
</html>

Adding a  Method to an object:

Adding a method to an existing object is also easy.

Example:
       myFather.name=function(){
                                     return this.firstName+ " "+ this. lastName;};
The method will be added to myFather not to myMother.
 
Adding Properties to a Prototype:

     You can not add a new property to a prototype the same way as you add a new property to an existing object, because the prototype is not an existing object.

       person.nationality="English";
To add a new property to a constructor, you must add it to the constructor function.

*Prototype properties can have prototype values (default values).
Adding methods to a prototype:
Constructor function can also define methods;

Example:
 function Person(first, last, age, eye) {
this.firstName=first;
this.lastName=last;
this.age=age;
this.eyecolor=eye;
this.name=function(){
                                     return this.firstName+ " "+ this. lastName;};
}

Using prototype property:
         The JavaScript prototype property allows us to add new properties to an existing prototype.
Example:
 function Person(first, last, age, eyecolor){
this.firstName=first;
this.lastName=last;
this.age=age;
this.eyecolor=eyecolor;
}
Person.prototype.nationality="English";
The JavaScript Prototype property also allows us to add new methods to an existing prototype.
function Person(first, last, age, eyecolor){
this.firstName=first;
this.lastName=last;
this.age=age;
this.eyecolor=eyecolor;
}
Person.prototype.name=function(){
return this.firstName+" "+this.lastName;
}
Only modify your own prototypes. Never modify the prototypes of standard JavaScript Objects.
    

No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...