when i was making a constructor in the previous lessons, i am infact making a new class.
A class can be thought of as a type, just like how 'number' and 'string' are types
when i made two objects bob and susan in previous lessons, they infact belong to class Person.
prototype keeps track of what a class can have property or method. the prototypes in javascript are automatically defined with a constructor.
*** if we want to add a method to class prototype, such that all the objects of this class can use the method. then we have to do this to extend the prototype
syntax:
classname.prototype.newmethod = function(){
statements;
};
function Dog(breed){
this.breeds = breed;
}
Dog.prototype.bark = function(){
console.log("woof");
}; // we added a bark method so other objects can use this method.
**Inheritance:
it allows one class to see and use the properties and methods of another class.
**DRY -- Don't Repeat Yourself principle of programming.
** to inherit class1 properties to class2, we need to set the prototype of class2 to be class1.
ex:
// the original Animal class and sayName method
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is "+this.name);
};
// define a Penguin class
function Penguin(name){
this.name = name;
this.numLegs = 2;
}
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
**the motif i noticed here is that to add any thing to a class like a method or inheritance of another class we use prototype.
* If javascript can't find something in the current class, it goes to the top of the prototype chain, to see if it from a class it inherited from.
This goes up the way upto the top Object.prototype . By default all classes directly inherit from Object class, unless we change the class's prototype.
A class can be thought of as a type, just like how 'number' and 'string' are types
when i made two objects bob and susan in previous lessons, they infact belong to class Person.
prototype keeps track of what a class can have property or method. the prototypes in javascript are automatically defined with a constructor.
*** if we want to add a method to class prototype, such that all the objects of this class can use the method. then we have to do this to extend the prototype
syntax:
classname.prototype.newmethod = function(){
statements;
};
function Dog(breed){
this.breeds = breed;
}
Dog.prototype.bark = function(){
console.log("woof");
}; // we added a bark method so other objects can use this method.
**Inheritance:
it allows one class to see and use the properties and methods of another class.
**DRY -- Don't Repeat Yourself principle of programming.
** to inherit class1 properties to class2, we need to set the prototype of class2 to be class1.
ex:
// the original Animal class and sayName method
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is "+this.name);
};
// define a Penguin class
function Penguin(name){
this.name = name;
this.numLegs = 2;
}
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
**the motif i noticed here is that to add any thing to a class like a method or inheritance of another class we use prototype.
* If javascript can't find something in the current class, it goes to the top of the prototype chain, to see if it from a class it inherited from.
This goes up the way upto the top Object.prototype . By default all classes directly inherit from Object class, unless we change the class's prototype.
No comments:
Post a Comment