不错的JavaScript面向对象的简单入门介绍第2/2页

JavaScript是一门OOP,而有些人说,JavaScript是基于对象的。

4) 方法
以下例子引用于<<__JavaScript: The Definitive Guide>>
function Rectangle_area( ) { return this.width * this.height; }
function Rectangle_perimeter( ) { return 2*this.width + 2*this.height; }
function Rectangle_set_size(w,h) { this.width = w; this.height = h; }
function Rectangle_enlarge( ) { this.width *= 2; this.height *= 2; }
function Rectangle_shrink( ) { this.width /= 2; this.height /= 2; }
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = Rectangle_area;
this.perimeter = Rectangle_perimeter;
this.set_size = Rectangle_set_size;
this.enlarge = Rectangle_enlarge;
this.shrink = Rectangle_shrink;
}
这种风格可能和Java,c++很不同。方法中的this表示调用它的对象的引用。


5) prototype
prototype是一个对象,每个类都包含一个prototype对象(注意,每个类一个,而不是每个对象一个)。
看看下面的例子:
function User(name)
{
this.name = name
}
User.prototype.name = "killercat" // 类名.prototype.属性(或方法)
user = new User("who"+"<br />")
document.write(user.name)
delete user.name
document.write(user.name)

再看一个例子:
function User(name)
{
}
User.prototype.name = "human"
user1 = new User()
user2 = new User()
document.write(user1.name+"<br />")
document.write(user2.name)
结果:
human
human
说明了,每个类一个prototype对象,而不是每个对象单独一个。

obj.x 这条语句的查找顺序是,先在obj中找x属性,假如没有,再进入obj对应的类中找prototype.x,对于方法来说,也一样。因此,不要出现这样的语句: user.prototype.name = "xxx" 必须是 user.name = "xxx" (prototype对象属于一个类,而不是一个对象)

类名.prototype.属性 // 相当于一个实例变量(属性),对方法也一样
类名.属性 // 相当于一个静态变量(属性),对方法也一样,调用的时候必须使用"类名.属性",不能使用"类对象.属性",因为它属于一个类,而不是一个对象。
例如:
function User(name)
{
this.name = name
}
User.type = "human"

user = new User("kc")
document.write(User.type + "<br />")
document.write(user.type)
结果:
human
undefined

另外,每个prototype都有一个constructor属性,默认用于保存constructor的定义,例如上面的user对象,调用:
user.constructor得到:
function User(name) { this.name = name; }
我们可以通过typeof,知道参数的类型,假如是对象,就返回"object",假如是方法就返回"function"

6) 利用prototype实现类间的继承,例如:
// 父类
function Circle(r){
this.r = r;

}
Circle.PI = 3.14;
Circle.prototype.getArea = function (){
return Circle.PI * this.r * this.r;
};
Circle.prototype.toString = function (){
if(( typeof this == "object") && (this.constructor == Circle)){
return "circle with a radius " + this.r ;
}
else{
return "unknown object";
}
};
Circle.max = function (c1,c2){
return c1.r >= c2.r ? c1 : c2;
};

// 子类
function ColorCircle(r,color){
this.r = r;
this.color = color;
}
ColorCircle.prototype = new Circle(0); // 保存父类的对象
ColorCircle.prototype.constructor = ColorCircle; // 为constructor 改名字
ColorCircle.prototype.toString = function(){
if(( typeof this == "object") && (this.constructor == ColorCircle)){
return this.color+" circle with a radius " + this.r ;
}
else{
return "unknown object";
}
}
ColorCircle.prototype.getColor = function(){
return this.color;
}
ColorCircle.prototype.setColor = function(color){
this.color = color;
}

也就是,使用prototype保存父类的对象,在构造子类的时候,父类对象同时被构造(因为prototype被构造)。也就是JavaScript继承其实就是让子类的prototype对象保存父类的对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值