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
28
29
30
31
32
33
js 本没有继承,只用js语法来实现继承效果<br>    function Person(age, name)
     {
         this.name =name;
         this.age = age;
     }
     Person.prototype.sayHi = function () {
         alert("age="+this.age+",name"+this.name);
     }
     var p1 = new Person(12, "ff");
     var p2 = new Person(21, "fffs");
     alert(p1.sayHi == p2.sayHi);
 
     function Father(name, age)
     {
         this.name = name;
         this.age = age;
     }
     Father.prototype.sayHi = function () {
         alert(this.name+","+this.age);
     }
 
     function Son(name,age,gender)
     {
         this.gender = gender;
         //call调用自己本身
         Father.call(this, name, age);//"继承" "父类"的成员
     }
     // Son.prototype = Father.prototype; //可以用父类的方法(这里有指针的问题)
     Son.prototype = new Father(); //得到父类对象
     Son.prototyp.fungame = function () { }; //prototype也是一个对象
     var f1 = new Father("JamesZou", 29);
     f1.sayHi();
     var s1 = new Son("jameszou", 1, true);

  

javascript模拟继承,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。