Enhanced Simple JavaScript inheritance (with)
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;
}
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
function Employee(name, salary) {
with(arguments.callee) {
if (prototype.__proto__ === Object.prototype) {
console.log('calling Person');
prototype.__proto__ = Person.prototype;
prototype.getSalary = function() {
return this.salary;
}
}
prototype.__proto__.constructor.call(this, name);
}
this.salary = salary;
}
function Executive(name, salary, bonus) {
with(arguments.callee) {
if (prototype.__proto__ === Object.prototype) {
console.log('calling Employee');
prototype.__proto__ = Employee.prototype;
prototype.getBonus = function() {
return this.bonus;
}
}
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) {
with(arguments.callee) {
if (prototype.__proto__ === Object.prototype) {
prototype.__proto__ = Person.prototype;
prototype.getSalary = function() {
return this.salary;
}
}
prototype.__proto__.constructor.call(this, name);
}
this.salary = salary;
}
function Executive(name, salary, bonus) {
with(arguments.callee) {
if (prototype.__proto__ === Object.prototype) {
prototype.__proto__ = Employee.prototype;
prototype.getBonus = function() {
return this.bonus;
}
}
prototype.__proto__.constructor.call(this, name, salary);
}
this.bonus = bonus;
}
alert('hi');
window.hotshot = new Executive('Hotshot', 400000, 100000);
window.workerbee = new Employee('Workerbee', 100000);