js完美继承代码示例

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <script>
 9     <!-- 定义继承函数-->
10 var inherits = function(subType,superType){
11     var F = function(){};
12     F.prototype = superType.prototype;
13     subType.prototype = new F();
14     subType.prototype.constructor = subType;
15 }
16 //定义父元素
17 function a(){
18     this.q = 2;
19 };
20 a.prototype.cc="ert";
21 //定义父元素;
22 function b(){
23 //a、 子元素内部调用call继承父元素的this的属性、方法
24     a.call(this)
25 };
26 //b、 运行继承函数继承prototype的属性、方法;
27 inherits(b,a);
28 var c = new b();
29     console.log(c.q)
30 </script>
31 </body>
32 </html>

 

posted @ 2015-11-13 11:15  挥刀  阅读(264)  评论(0编辑  收藏  举报