Last active
September 23, 2016 20:09
-
-
Save Rhoxio/58205b8ba0264e0769725c058d4b3cc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////// | |
// Constructors // | |
////////////////////// | |
// This is a constructor. When you call 'new Person', it evokes this function and returns an object with whatever properties got assigned to 'this'. | |
var Person = function(name, age, weight){ | |
this.name = name | |
this.age = age | |
this.weight = weight | |
this.location = "Earth" | |
// These functions will be passed along when you inherit attributes from this object since they are defined on 'this'. | |
this.consume = function(food){ | |
if(food){ | |
console.log(this.name+" is no longer hungry.") | |
} else { | |
console.log(this.name+"'s stomach rumbles furiously.") | |
} | |
} | |
this.greet = function(){ | |
console.log("Hey, my name is "+this.name) | |
} | |
} | |
Person.prototype = { | |
complain: function(){ | |
if(this.hasOwnProperty("college")){ | |
console.log("I have no time for fun!") | |
} else { | |
console.log("I can't believe that I am "+this.weight+" lbs.") | |
} | |
} | |
} | |
// Can also do Person.prototype.complain = function(){ code } | |
// Instantiate a new instance of a person. | |
var kevin = new Person("Kevin", 25, 155) | |
// console.log(kevin.complain()) | |
// console.log(kevin instanceof Person) | |
//////////////////// | |
// Inheriting // | |
//////////////////// | |
// This is a constructor that takes a parent object. (the 'person' argument in this case) | |
// We expect it to get it's prototype from a 'new Person'. | |
function Astronaut(person, college, genetics, location, callback){ | |
// First argument in call() is context. The rest are the parent (Person) fuction's arguments. | |
// Because the constructor is a function, you can use 'bind', 'apply', and 'call' to set the context of 'this' | |
// in which the function is executed and attributes on 'this' get assigned. | |
// You could just as easily set 'this' to any other object, but in this case we want | |
// it to set the 'this' properties on the 'new Astronaut' as they would be on a 'new Person'. | |
Person.call(this, person.name, person.age, person.weight); | |
// If you console.log out 'this' right here, you'll see it only has a person's properties on it. | |
// console.log(this) | |
// Now we set more properties on 'this'. You can reassign values inherited from Person as well | |
// and override inherited functions as well. They are just attributes on the object at this point. | |
this.profession = "astronaut" | |
this.location = location | |
this.college = college | |
this.genetics = genetics | |
// Callback if you need any other actions to happen before construction is finished. | |
// Passing 'this' through will allow you to amend the object or call functions on it. | |
callback(this) | |
} | |
// Set the prototype of the Astronaut contructor to Person. | |
// This will allow an Astronaut access to the 'complain' function which it does NOT inherit when using Person.call(). | |
Astronaut.prototype = new Person | |
var spaceman = new Astronaut(kevin, true, "Good", "Moon", function(person){ | |
person.genetics = "Robust" | |
// Prototype functions or attributes can be called without explicitly stating 'Object.prototype.functionName' | |
// So you can just use Object.function. | |
person.complain() | |
}) | |
console.log(spaceman) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment