Created
January 25, 2015 11:26
-
-
Save kenjiSpecial/c0ba9880e59720d3415d to your computer and use it in GitHub Desktop.
object oriented javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript http://phrogz.net/JS/classes/OOPinJS2.html
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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript | |
// Define the Person constructor | |
var Person = function(firstName) { | |
this.firstName = firstName; | |
}; | |
// Add a couple of methods to Person.prototype | |
Person.prototype.walk = function(){ | |
console.log("I am walking!"); | |
}; | |
Person.prototype.sayHello = function(){ | |
console.log("Hello, I'm " + this.firstName); | |
}; | |
// Define the Student constructor | |
function Student(firstName, subject) { | |
// Call the parent constructor, making sure (using Function#call) | |
// that "this" is set correctly during the call | |
Person.call(this, firstName); | |
// Initialize our Student-specific properties | |
this.subject = subject; | |
}; | |
// Create a Student.prototype object that inherits from Person.prototype. | |
// Note: A common error here is to use "new Person()" to create the | |
// Student.prototype. That's incorrect for several reasons, not least | |
// that we don't have anything to give Person for the "firstName" | |
// argument. The correct place to call Person is above, where we call | |
// it from Student. | |
Student.prototype = Object.create(Person.prototype); // See note below | |
// Set the "constructor" property to refer to Student | |
Student.prototype.constructor = Student; | |
// Replace the "sayHello" method | |
Student.prototype.sayHello = function(){ | |
console.log("Hello, I'm " + this.firstName + ". I'm studying " | |
+ this.subject + "."); | |
}; | |
// Add a "sayGoodBye" method | |
Student.prototype.sayGoodBye = function(){ | |
console.log("Goodbye!"); | |
}; | |
// Example usage: | |
var student1 = new Student("Janet", "Applied Physics"); | |
student1.sayHello(); // "Hello, I'm Janet. I'm studying Applied Physics." | |
student1.walk(); // "I am walking!" | |
student1.sayGoodBye(); // "Goodbye!" | |
// Check that instanceof works correctly | |
console.log(student1 instanceof Person); // true | |
console.log(student1 instanceof Student); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment