Created
September 16, 2014 17:46
-
-
Save raghothams/1b6b9023ad9013355c05 to your computer and use it in GitHub Desktop.
Javascript inheritence
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
| { | |
| "name": "Inheritence", | |
| "cells": [ | |
| { | |
| "in": "// prototypal inheritence\n// http://oli.me.uk/2013/06/01/prototypical-inheritance-done-right/\n", | |
| "out": "3" | |
| }, | |
| { | |
| "in": "function Vehicle(name){\n\tthis.name = name;\n}\n\nVehicle.prototype.getName = function(){\n\treturn this.name;\n}\n\nvar veh1 = new Vehicle('lorry');\nconsole.log(veh1.getName())\n", | |
| "out": "lorry\nundefined" | |
| }, | |
| { | |
| "in": "function car(){}\n\ncar.prototype = Vehicle.prototype;\ncar.prototype.colour = \"none\";\ncar.prototype.setColour = function(colour){\n\tthis.color = colour;\n}\n\nveh1.colour;", | |
| "out": "'none'" | |
| }, | |
| { | |
| "in": "// observe the prorotype of vehicle is changed & hence even veh1 got the property colour\n// but we only wanted the colour property for the car objects\n\n// we can overcome this problem by using Object.create", | |
| "out": "" | |
| }, | |
| { | |
| "in": "function Vehicle(name){\n\tthis.name = name;\n}\n\nVehicle.prototype.getName = function(){\n\treturn this.name;\n}\n\nvar veh1 = new Vehicle('lorry');\nconsole.log(veh1.getName())\n\nfunction Car(){}\n\n// use Object.create to copy prototype\nCar.prototype = Object.create(Vehicle.prototype);\nCar.prototype.colour = \"none\";\nCar.prototype.setColour = function(colour){\n\tthis.colour = colour;\n}\n\nveh1.colour;", | |
| "out": "lorry\nundefined" | |
| }, | |
| { | |
| "in": "var car1 = new Car(\"Benz\");\ncar1.setColour(\"black\");\ncar1.colour", | |
| "out": "'black'" | |
| }, | |
| { | |
| "in": "// one more problem !\nveh1.constructor;\n", | |
| "out": "[Function: Vehicle]" | |
| }, | |
| { | |
| "in": "car1.constructor;", | |
| "out": "[Function: Vehicle]" | |
| }, | |
| { | |
| "in": "// wtf!!!", | |
| "out": "" | |
| }, | |
| { | |
| "in": "function Vehicle(name){\n\tthis.name = name;\n}\n\nVehicle.prototype.getName = function(){\n\treturn this.name;\n}\n\nvar veh1 = new Vehicle('lorry');\n\n\nfunction Car(){}\n\n// use Object.create to copy prototype\nCar.prototype = Object.create(Vehicle.prototype);\nCar.prototype.constructor = Car;\nCar.prototype.colour = \"none\";\nCar.prototype.setColour = function(colour){\n\tthis.colour = colour;\n}\n\n", | |
| "out": "[Function]" | |
| }, | |
| { | |
| "in": "var car1 = new Car(\"Benz\");\ncar1.setColour(\"black\");\ncar1.colour", | |
| "out": "'black'" | |
| }, | |
| { | |
| "in": "veh1.constructor;", | |
| "out": "[Function: Vehicle]" | |
| }, | |
| { | |
| "in": "car1.constructor;", | |
| "out": "[Function: Car]" | |
| }, | |
| { | |
| "in": "veh1 instanceof Vehicle;", | |
| "out": "true" | |
| }, | |
| { | |
| "in": "car1 instanceof Vehicle;", | |
| "out": "true" | |
| }, | |
| { | |
| "in": "// how to use methods from base prototype when we override?", | |
| "out": "" | |
| }, | |
| { | |
| "in": "function Vehicle(name){\n\tthis.name = name;\n}\n\nVehicle.prototype.getName = function(){\n\treturn this.name;\n}\n\nvar veh1 = new Vehicle('lorry');\n\n\nfunction Car(name){\n\tVehicle.call(this, name);\n}\n\n// use Object.create to copy prototype\nCar.prototype = Object.create(Vehicle.prototype);\nCar.prototype.constructor = Car;\nCar.prototype.colour = \"none\";\nCar.prototype.setColour = function(colour){\n\tthis.colour = colour;\n}\n\nCar.prototype.getName = function(){\n\tvar baseRes = Vehicle.prototype.getName.call(this);\n\treturn \"Car is \"+baseRes;\n}\n", | |
| "out": "[Function]" | |
| }, | |
| { | |
| "in": "var car2 = new Car(\"BMW\");\nconsole.log(car2.getName())", | |
| "out": "Car is BMW\nundefined" | |
| }, | |
| { | |
| "in": "car2 instanceof Vehicle;", | |
| "out": "true" | |
| } | |
| ] | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment