Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Created November 14, 2016 02:04
Show Gist options
  • Save deepakshrma/6b966c5384bb31a9f60b13e36871ccce to your computer and use it in GitHub Desktop.
Save deepakshrma/6b966c5384bb31a9f60b13e36871ccce to your computer and use it in GitHub Desktop.
Javascript Inheritance
/**
* Created by intelligrape on 17/7/14.
*/
Function.prototype.inherits = function (parent) {
"use strict";
this.prototype = new parent();
this.constructor = this;
this.prototype.parent = parent.prototype
}
function User(name) {
"use strict";
var _uname = name;
this.getName = function () {
return _uname;
}
this.uinfo = "I am parent"; //public
}
User.prototype.getInfo = function () {
"use strict";
return this.uinfo;
}
function Employee(name, salary) {
"use strict";
var _uname = name;
this.getName = function () {
return _uname;
}
this.salary = 100;
this.getSalary = function () {
return salary;
}
this.setSalary = function (sal) {
salary = sal;
}
this.uinfo = "I am child...";
}
Employee.inherits(User)
var user = new User("deepak");
console.log(user.getName())
console.log(user.getInfo())
var employee = new Employee("rishabh");
console.log(employee.getName())
console.log(employee.getInfo())
//console.log(employee.constructor.parent.getDatacall())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment