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            *    练习者: Alex刺客
10            *    日期: 2009-12-13
11            */

12            
13            /*
14                1.对象冒充
15            */

16            //ClassA类
17            function ClassA (sColor) {
18                this.color = sColor;
19                this.sayColor = function () {
20                        alert(this.color);
21                    }

22            }

23            
24            //ClassB类
25            function ClassB(sColor,sName){
26                //当前对象的属性是ClassA函数的指针
27                this.newMethod = ClassA;
28                //把参数传递给它
29                this.newMethod(sColor);
30                //删除当前对象的ClassA函数的指针
31                delete this.newMethod;
32                
33                //新增属性和方法
34                this.name = sName;
35                this.sayName = function () {
36                    alert(this.name);
37                }

38            }

39            
40            //var cb = new ClassB("blue!","Redboy");
41            //cb.sayColor();
42            //cb.sayName();
43            
44            /*
45                call()方法
46                call()方法与对象冒充方法最相似。它的第一个参数用作this的对象。
47                其他参数都直接传递给函数自身。
48            */

49            //ClassC类
50            function ClassC(sColor,sName){
51                //this.newMethod = ClassA;
52                //this.newMethod(sColor);
53                //delete this.newMethod;
54                ClassA.call(this,sColor); //以上三行代码由这行替代
55                
56                //新增属性和方法
57                this.name = sName;
58                this.sayName = function () {
59                    alert(this.name);
60                }

61            }

62            
63            //var cc = new ClassC("blue","c");
64            //cc.sayColor();
65            //cc.sayName();
66            
67            /*
68                apply()方法
69                apply()方法有两个参数,跟call()方法相似,只是第二个参数变成了数组。
70            */

71            //ClassD类
72            function ClassD(sColor,sName){
73                //this.newMethod = ClassA;
74                //this.newMethod(sColor);
75                //delete this.newMethod;
76                ClassA.apply(this,new Array(sColor)); //以上三行代码由这行替代
77                
78                //新增属性和方法
79                this.name = sName;
80                this.sayName = function () {
81                    alert(this.name);
82                }

83            }

84            
85            //var dt = new ClassD("red","blueBoy");
86            //dt.sayColor();
87            //dt.sayName();
88            
89        </script>
90    </head>
91    <body>
92    </body>
93</html>
posted on 2009-12-13 23:10 Alex刺客 阅读(247) 评论(0)  编辑  收藏 所属分类: JavaScript

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


网站导航: