typing - GradedJestRisk/js-training GitHub Wiki

Table of Contents

Overview

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 ?
More:

Constructor

get

Get it with dog.constructor.name

set

const Dog = function(name){
    this.name = name;
};

Prototype

Dog.prototype.species = 'Canine';
Dog.prototype.bark = function(){ return("woof!"); };

Instance

const dog = new Dog('Fluffy');

Multiple inheritance

Constructor

// Supply 
// * parameters for inherited type
// * parameters for current type
const ShowDog = function (name, breed){
	this.name = name;
	this.breed = breed;
};

Prototype

// 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';}

Instance

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());
⚠️ **GitHub.com Fallback** ⚠️