Classical Inheritance in JavaScript : Reassigning the constructor -
i trying learn inheritance in javascript using prototype keyword.
i came code across web-site explained me classical inheritance in javascript. using mozilla rhino command-line javascript
this code
var fn55 = function(){ var employee = function(name){ var name = name; this.getname = function(){ return name; }; this.setname = function(empname){ name = empname; }; }; var contractemp = function(name,sal){ var salary = sal; this.getsalary = function(){ return salary; } //calling super constructor employee.apply(this,[name]); }; contractemp.prototype = new employee(); contractemp.prototype.constructor = contractemp; var emp1 = new contractemp("jack",3000); var emp2 = new contractemp("john",4000); print(emp1.getname()); print(emp2.getname()); print(emp1.getname()); employee.prototype.getinfo = function(){ return "emp name \""+this.getname()+"\" salary "+this.getsalary(); } print(emp1.getinfo()); }; fn55();
the output
jack john jack emp name "jack" salary 3000
now if comment line in code
//contractemp.prototype.constructor = contractemp;
the output remains is.
so question is, whats purpose of re-assigning constructor
so question is, whats purpose of re-assigning constructor
the purpose reset constructor
original value.
Comments
Post a Comment