summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2017-03-01 22:47:47 -0600
committerKyle K <kylek389@gmail.com>2017-03-01 22:47:47 -0600
commite6620b104d5349c1ccdc26508d5df8f8ae7401b6 (patch)
tree7efe7b6130c17df8b8d89ee75c641873dca1e63a
parentb4062724b3e932e91a81e7db63732d6091e314a4 (diff)
downloadjsexamples-e6620b104d5349c1ccdc26508d5df8f8ae7401b6.tar.gz
jsexamples-e6620b104d5349c1ccdc26508d5df8f8ae7401b6.tar.bz2
jsexamples-e6620b104d5349c1ccdc26508d5df8f8ae7401b6.zip
example of inheritance in JS
-rw-r--r--inheritance.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/inheritance.js b/inheritance.js
new file mode 100644
index 0000000..2fab447
--- /dev/null
+++ b/inheritance.js
@@ -0,0 +1,29 @@
+function Person(name){
+ this.name = name;
+ this.type = 'human';
+ this.info = function() {
+ console.log("Name:", this.name, "Type:", this.type);
+ }
+}
+Person.prototype.roboinfo = function(){
+ console.log("Name:", this.name, "Type:", this.type, "Processor:", this.cpu);
+}
+
+function Robot(name){
+ Person.call(this, name) // akin of calling super, this is what allows us to inherit props
+ this.type = 'robot';
+ this.cpu = 'AMD64'
+}
+
+// Set Robot's prototype to Person's prototype
+Robot.prototype = Person.prototype;
+
+// Set constructor back to Robot
+Robot.prototype.constructor = Robot;
+
+person = new Person("Bob");
+robot = new Robot("Bender");
+
+person.info(); // Name: Bob Type: human
+robot.info(); // Name: Bender Type: robot
+robot.roboinfo(); // Name: Bender Type: robot Processor: AMD64 \ No newline at end of file