Skip to content

Instantly share code, notes, and snippets.

@erockdotdev
Created May 8, 2017 20:51
Show Gist options
  • Save erockdotdev/90214fe6122c294635c816f547137775 to your computer and use it in GitHub Desktop.
Save erockdotdev/90214fe6122c294635c816f547137775 to your computer and use it in GitHub Desktop.
prototype_lab

//////////////////////////////////////////// ////// PART 1 ////////////////////////////// ////////////////////////////////////////////

// Add comments to the below code and explain // what is happening

function Vehicle(type) { this.type = type; this.displayType = function() { console.log('this is a type of', this.type) }; }

function Car(make, manufacturer) { Vehicle.call(this,'car'); this.make = make; this.manufacturer = manufacturer; }

let v1 = new Car('sedan','ford');

//////////////////////////////////////////// ////// PART 1 ////////////////////////////// ////////////////////////////////////////////

Open a new Repl and paste the following code. Work through the steps to create the constructors, instantiate objects and work with inheritance.

Include the link to your repl here: link to repl

// Create a Human constructor

function Human(name, strength){

// add the following properties: name and strength

this.name = name; this.strength = strength;

// Add a 'speak' method that console.logs a greeting with that objects name

this.speak = function(){ console.log('hi '+ this.name) }

}

// set strength = 'normal'

// Instantiate new instance of the Human constructor with your name

let human = new Human('eric','normal') //human

// Make the new object execute the speak method human.speak()

//////////////////////////////////////////// ////// REFACTOR 1 ////////////////////////// ////////////////////////////////////////////

// Refactor the Human constructor and remove the speak method // and add it to it's prototype.

// Make sure to confirm that the Human can speak

//////////////////////////////////////////// ////// REFACTOR 2 ////////////////////////// ////////////////////////////////////////////

// Refactor the Human prototype and remove the speak method // and add it to Javascripts top level Object's prototype // Create a Human constructor

function Human(name, strength){

// add the following properties: name and strength

this.name = name; this.strength = strength; }

Human.prototype.speak = function(){ console.log('hi '+ this.name) }

let human = new Human('eric','normal') //human

// Make the new object execute the speak method human.speak()

// Make sure to confirm that the Human can speak

//////////////////////////////////////////// ////// REFACTOR 3 ////////////////////////// ////////////////////////////////////////////

// Remove the speak prototype from the Object and // add it back to the Human prototype

//////////////////////////////////////////// ////// superHuman constructor ////////////// ////////////////////////////////////////////

function SuperHuman(){ human.call() villians = []; } SuperHuman.prototype.fly = function(){ console.log("it's a bird; it's a plane; it's " + this.name) }

// Create a superHuman constructor that // inherits the name property from the Human constructor // Add a strength property that is set to 'super normal'

// Extend the superHuman prototype to inherit from the Human prototype

// Extend the superHuman prototype to include an array called 'villans'

// Extend the superHuman prototype to include a method called fly that console.logs: // "it's a bird; it's a plane; it's " + this.name

// Instantiate a new instance of superHuman called "supermane"

// Make superman speak

// Make superman fly

//////////////////////////////////////////// ////// Bonus /////////////////////////////// ////////////////////////////////////////////

/* BONUS:

  1. Create a new superhuman called thor
  2. Add a new villan to the villan array
  3. Return the list of villans
  4. Add a new prototype method of your choosing and make sure that both */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment