Skip to content

Instantly share code, notes, and snippets.

@carlosvega20
Created December 4, 2015 06:10
Show Gist options
  • Save carlosvega20/a683e771a38039f5622c to your computer and use it in GitHub Desktop.
Save carlosvega20/a683e771a38039f5622c to your computer and use it in GitHub Desktop.
prototype chain
//Inheritance
var Mass = function () {this.material = "rock"; console.log("init Mass")};
var Planet = function (name) {Mass.call(this); this.name = name || "Planet"; console.log("init "+this.name)};
Planet.prototype = Object.create(Mass.prototype);
Planet.prototype.constructor = Planet;
var earth = new Planet("earth");
// Second Way
function Father () {this.type="Human";}
function Son (name) {this.name=name;}
Son.prototype = new Father();
var carlos = New Son ('carlos');
//console.log(carlos)
/*
Son {name: "carlos"}
name: "carlos"
__proto__: Father
type: "Human"
__proto__: Father
constructor: function Father()
__proto__: Object
*/
//console.log(earth)
/*
Planet {material: "rock", name: "earth"}
material: "rock"
name: "earth"
__proto__: Planet
constructor: (name)
__proto__: Mass
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment