一个简单的Javascript的继承案例

 1 function Person(name, age) {
 2     this.name = name;
 3     this.age = age;
 4 }
 5 function Male(name, age) {
 6     Person.apply(this, arguments);
 7     this.sex = '男';
 8 }
 9 
10 Male.prototype.showSex = function(){
11     console.log(this.sex);
12 }
13 function F(){}
14 F.prototype = Person.prototype;
15 Male.prototype = new F();
16 Male.prototype.constructor = Male;
17 var m = new Male("小明", 20);
18 Person.prototype.showName = function(){console.log(this.name);}
19 m.showName();
20 console.log(m.constructor == Male);
21 console.log(m instanceof Person);
22 console.log(m instanceof Male);
23 console.log(m instanceof F);

 

posted @ 2013-08-02 10:32  Jackin  阅读(226)  评论(0编辑  收藏  举报