[javascript]一种比较通用的js继承过程
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function Person(name, gender) { this.name = name; this.gender = gender; } Person.prototype.showName = function() { alert(this.name); } Person.prototype.showGender =function() { alert(this.gender); } function Worker(name, gender, job) {
// Step1 Person.call(this, name, gender); this.job = job; } // Step2 for (var i in Person.prototype) { Worker.prototype[i] = Person.prototype[i]; } Worker.prototype.showJob = function() { alert(this.job); } var oW1 = new Worker("hello", "male", "coder"); oW1.showJob(); </script> </body> </html>