最爱Java

书山有路勤为径,学海无涯苦作舟

javascript面向对象技术基础(五)

        本文转载于javaeye(http://www.javaeye.com/wiki/Object_Oriented_JavaScript/1279-javascript-object-oriented-technology-one),只进行了重新排版以便收藏。
        文中所有英文语句(程序语句除外),都引自<<javascript-the definitive guide,5th edition>>。

------------------------------------------------------------------------------------
类变量/类方法/实例变量/实例方法
        先补充一下以前写过的方法:
        在javascript中,所有的方法都有一个call方法和apply方法。这两个方法可以模拟对象调用方法。它的第一个参数是对象,后面的参数表示对象调用这个方法时的参数(ECMAScript specifies two methods that are defined for all functions, call() and apply(). These methods allow you to invoke a function as if it were a method of some other object. The first argument to both call() and apply() is the object on which the function is to be invoked; this argument becomes the value of the this keyword within the body of the function. Any remaining arguments to call() are the values that are passed to the function that is invoked)。比如我们定义了一个方法f(),然后调用下面的语句:
    f.call(o, 1, 2);
作用就相当于
    o.m = f;
    o.m(1,2);
    delete o.m;
举个例子:
 1function Person(name,age) {  //定义方法   
 2    this.name = name;   
 3    this.age = age;   
 4}
   
 5var o = new Object();   //空对象   
 6alert(o.name + "_" + o.age); //undefined_undefined   
 7  
 8Person.call(o,"sdcyst",18); //相当于调用:o.Person("sdcyst",18)   
 9alert(o.name + "_" + o.age); //sdcyst_18   
10  
11Person.apply(o,["name",89]);//apply方法作用同call,不同之处在于传递参数的形式是用数组来传递   
12alert(o.name + "_" + o.age); //name_89  

---------------------------------

实例变量和实例方法都是通过实例对象加"."操作符然后跟上属性名或方法名来访问的,但是我们也可以为类来设置方法或变量,
这样就可以直接用类名加"."操作符然后跟上属性名或方法名来访问。定义类属性和类方法很简单:

 1Person.counter = 0;   //定义类变量,创建的Person实例的个数
 2function Person(name,age) {
 3    this.name = name;
 4    this.age = age;
 5    Person.counter++//没创建一个实例,类变量counter加1
 6}
;
 7
 8Person.whoIsOlder = function(p1,p2) //类方法,判断谁的年龄较大
 9    if(p1.age > p2.age) {
10        return p1;
11    }
 else {
12        return p2;
13    }

14}

15
16var p1 = new Person("p1",18);
17var p2 = new Person("p2",22);
18
19alert("现在有 " + Person.counter + "个人");  //现在有2个人
20var p = Person.whoIsOlder(p1,p2);
21alert(p.name + "的年龄较大");   //p2的年龄较大

prototype属性的应用:
下面这个例子是根据原书改过来的.
假设我们定义了一个Circle类,有一个radius属性和area方法,实现如下:

1function Circle(radius) {
2    this.radius = radius;
3    this.area = function() {
4        return 3.14 * this.radius * this.radius;
5    }

6}

7var c = new Circle(1);
8alert(c.area());  //3.14

     假设我们定义了100个Circle类的实例对象,那么每个实例对象都有一个radius属性和area方法。实际上,除了radius属性,每个Circle类的实例对象的area方法都是一样,这样的话,我们就可以把area方法抽出来定义在Circle类的prototype属性中,这样所有的实例对象就可以调用这个方法,从而节省空间。

1function Circle(radius) {
2    this.radius = radius;
3}

4Circle.prototype.area = function() {
5        return 3.14 * this.radius * this.radius;
6    }

7var c = new Circle(1);
8alert(c.area());  //3.14

现在,让我们用prototype属性来模拟一下类的继承:首先定义一个Circle类作为父类,然后定义子类PositionCircle。

 1function Circle(radius) {  //定义父类Circle
 2    this.radius = radius;
 3}

 4Circle.prototype.area = function() //定义父类的方法area计算面积
 5    return this.radius * this.radius * 3.14;
 6}

 7
 8function PositionCircle(x,y,radius) //定义类PositionCircle
 9    this.x = x;                    //属性横坐标
10    this.y = y;                       //属性纵坐标
11    Circle.call(this,radius);      //调用父类的方法,相当于调用this.Circle(radius),设置PositionCircle类的
12                                   //radius属性
13}

14PositionCircle.prototype = new Circle(); //设置PositionCircle的父类为Circle类
15
16var pc = new PositionCircle(1,2,1);
17alert(pc.area());  //3.14
18                   //PositionCircle类的area方法继承自Circle类,而Circle类的
19                   //area方法又继承自它的prototype属性对应的prototype对象
20alert(pc.radius); //1  PositionCircle类的radius属性继承自Circle类
21
22/*
23注意:在前面我们设置PositionCircle类的prototype属性指向了一个Circle对象,
24因此pc的prototype属性继承了Circle对象的prototype属性,而Circle对象的constructor属
25性(即Circle对象对应的prototype对象的constructor属性)是指向Circle的,所以此处弹出
26的是Circ.
27*/

28alert(pc.constructor); //Circle    
29
30/*为此,我们在设计好了类的继承关系后,还要设置子类的constructor属性,否则它会指向父类
31的constructor属性
32*/

33PositionCircle.prototype.constructor = PositionCircle
34alert(pc.constructor);  //PositionCircle

posted on 2009-10-20 11:34 Brian 阅读(206) 评论(0)  编辑  收藏 所属分类: JScript


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


网站导航:
 

公告


导航

<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(4)

随笔分类

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜