javascript一种比较通用的继承过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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>