Enhanced Simple JavaScript inheritance
Demostrates the blog entry at
http://sandipchitale.blogspot.com/2014/03/enhanced-simple-javascript-inheritance.html
by sandipchitale
HTML
<p>Click on Evalute link to evaluate various expressions on <code>hotshot</code> and <code>workerbee</code>. Open your devtools console to see the output to console.</p>
<input id="evaluate" value="console.dir(hotshot);" size="80" /> <a href="javascript:window.clickit()">Evaluate</a>
<pre>
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
};
function Employee(name, salary) {
if (arguments.callee.prototype.__proto__ === Object.prototype) {
console.log('calling Person');
arguments.callee.prototype.__proto__ = Person.prototype;
arguments.callee.prototype.getSalary = function() {
return this.salary;
}
}
arguments.callee.prototype.__proto__.constructor.call(this, name);
this.salary = salary;
};
function Executive(name, salary, bonus) {
if (arguments.callee.prototype.__proto__ === Object.prototype) {
console.log('calling Employee');
arguments.callee.prototype.__proto__ = Employee.prototype;
arguments.callee.prototype.getBonus = function() {
return this.bonus;
}
}
arguments.callee.prototype.__proto__.constructor.call(this, name, salary);
this.bonus = bonus;
};
var hotshot = new Executive('Hotshot', 400000, 100000);
var workerbee = new Employee('Workerbee', 100000);
</pre>
JavaScript
window.clickit = function () {
eval(document.getElementById("evaluate").value);
return false;
};
function Person(name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
};
function Employee(name, salary) {
if (arguments.callee.prototype.__proto__ === Object.prototype) {
arguments.callee.prototype.__proto__ = Person.prototype;
arguments.callee.prototype.getSalary = function () {
return this.salary;
};
};
arguments.callee.prototype.__proto__.constructor.call(this, name);
this.salary = salary;
}
function Executive(name, salary, bonus) {
if (arguments.callee.prototype.__proto__ === Object.prototype) {
arguments.callee.prototype.__proto__ = Employee.prototype;
arguments.callee.prototype.getBonus = function () {
return this.bonus;
}
}
arguments.callee.prototype.__proto__.constructor.call(this, name, salary);
this.bonus = bonus;
}
window.hotshot = new Executive('Hotshot', 400000, 100000);
window.workerbee = new Employee('Workerbee', 100000);