typing - GradedJestRisk/js-training GitHub Wiki
List:
- constructors are factories for objets
- they take individual characteristics (usually, properties) as parameters to create the object
- they also give each object access to common properties (kind of Java class properties) or operations (kind of Java class methods) stored in prototype
- can you create a prototype before the constructor ?
Get it with dog.constructor.name
const Dog = function(name){
this.name = name;
};
Dog.prototype.species = 'Canine';
Dog.prototype.bark = function(){ return("woof!"); };
const dog = new Dog('Fluffy');
// Supply
// * parameters for inherited type
// * parameters for current type
const ShowDog = function (name, breed){
this.name = name;
this.breed = breed;
};
// No arguments to constructor kin of copy all from Dog)
ShowDog.prototype = new Dog;
// We handled the construction above in ShowDog
ShowDog.prototype.constructor = ShowDog;
// Prototype (properties and methods)
ShowDog.prototype.league = "London";
ShowDog.prototype.stack = function() { return 'stacking';}
aShowDog = new ShowDog('pixy', 'Terrier');
console.log("My name is " + aShowDog.name + "I'm a " + aShowDog.breed);
console.log("Sending stack to pixy: " + aShowDog.stack());