Alex刺客

Dancing fingers, damage world. -- 舞动手指,破坏世界.

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  57 随笔 :: 0 文章 :: 76 评论 :: 0 Trackbacks
 1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2<html xmlns="http://www.w3.org/1999/xhtml">
 3    <head>
 4        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5        <title>原型链方式</title>
 6        <script type="text/javascript">
 7            /*
 8            *    项目: book -> Javascript高级程序设计.pdf -> 第四章 -> 4.2.1 继承的方式
 9            *    说明:使用prototype属性
10            *    练习者: Alex刺客
11            *    日期: 2009-12-13
12            */

13            
14            /*
15                原型链方式
16                原型链的神奇之处在于突出显示的代码,这里把ClassB的prototype属性设置成ClassA的实例。
17                这很有意义,因为想要ClassA的所有属性和方法。所以把ClassB的全部属性设置成ClassA的实例。
18                因为这种继承方式使用了prototype属性,所以instanceof运算符可以正确运行。
19            */

20            function ClassA () {}
21            
22            ClassA.prototype.color = 'red';
23            ClassA.prototype.sayColor = function () {
24                alert(this.color);
25            }

26            
27            function ClassB () {}
28            ClassB.prototype = new ClassA();
29            //添加新方法
30            ClassB.prototype.name = "ClassB";
31            ClassB.prototype.sayName = function () {
32                alert(this.name);
33            }

34            
35            /*
36                混合方式 对象冒充+原型链
37                跟建造类一样的问题也出现在继承当中,所以也就产生了这种方式。
38                用对象冒充继承构造函数,用原型链继承prototype对象的方法。
39            */

40            
41            function ClassD ( sColor) {
42                this.color = sColor;
43                if(typeof ClassD._initMethod == "undefined"{
44                    ClassD.prototype.sayColor = function () {
45                        alert(this.color);
46                    }

47                    alert('ClassD我只生成一次!');
48                    ClassD._initMethod = true;
49                }

50            }

51            var cd = new ClassD();
52            function ClassE (sColor, sName) {
53                ClassD.call(this,sColor);
54                this.name = sName;
55                if(typeof ClassE._initMethod == "undefined"{
56                    ClassE.prototype.sayName =function () {
57                        alert(this.name);
58                    }

59                    alert('ClassE我只生成一次!');
60                    ClassE._initMethod = true;
61                }

62            }

63            ClassE.prototype = new ClassD();
64            /*
65                继承机制不能采用动态化的原因是,prototype对象的唯一性。如果放入 if 区域 
66                在代码运行前,对象已被实例化了,并与原始的prototype对象联系在一起。虽然用极
67                晚绑定可使对原型对象的修改正确地返映出来,但是替换prototype对象却不会对该对象
68                产生任何影响。只有未来的对象实例才会反映出这种改变,这就使第一个实例变得不正确。
69                
70            */

71            
72            var ce1 = new ClassE("red","blueBoy");
73            var ce2 = new ClassE("blue","redBoy");
74            ce1.sayColor();
75            ce1.sayName();
76            ce2.sayColor();
77            ce2.sayName();
78            
79            
80        </script>
81    </head>
82    <body>
83    </body>
84</html>
posted on 2009-12-13 23:11 Alex刺客 阅读(275) 评论(0)  编辑  收藏 所属分类: JavaScript

只有注册用户登录后才能发表评论。


网站导航: