最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

不錯(cuò)的JavaScript面向?qū)ο蟮暮?jiǎn)單入門(mén)介紹第2/2頁(yè)

 更新時(shí)間:2008年07月03日 20:50:41   作者:  
JavaScript是一門(mén)OOP,而有些人說(shuō),JavaScript是基于對(duì)象的。

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;
}
這種風(fēng)格可能和Java,c++很不同。方法中的this表示調(diào)用它的對(duì)象的引用。


5) prototype
prototype是一個(gè)對(duì)象,每個(gè)類(lèi)都包含一個(gè)prototype對(duì)象(注意,每個(gè)類(lèi)一個(gè),而不是每個(gè)對(duì)象一個(gè))。
看看下面的例子:
function User(name)
{
    this.name = name
}
User.prototype.name = "killercat"  // 類(lèi)名.prototype.屬性(或方法)
user = new User("who"+"<br />")
document.write(user.name)
delete user.name
document.write(user.name)

再看一個(gè)例子:
function User(name)
{
}
User.prototype.name = "human"
user1 = new User()
user2 = new User()
document.write(user1.name+"<br />")
document.write(user2.name)
結(jié)果:
human
human
說(shuō)明了,每個(gè)類(lèi)一個(gè)prototype對(duì)象,而不是每個(gè)對(duì)象單獨(dú)一個(gè)。

obj.x 這條語(yǔ)句的查找順序是,先在obj中找x屬性,假如沒(méi)有,再進(jìn)入obj對(duì)應(yīng)的類(lèi)中找prototype.x,對(duì)于方法來(lái)說(shuō),也一樣。因此,不要出現(xiàn)這樣的語(yǔ)句: user.prototype.name = "xxx" 必須是 user.name = "xxx" (prototype對(duì)象屬于一個(gè)類(lèi),而不是一個(gè)對(duì)象)

類(lèi)名.prototype.屬性  // 相當(dāng)于一個(gè)實(shí)例變量(屬性),對(duì)方法也一樣
類(lèi)名.屬性  // 相當(dāng)于一個(gè)靜態(tài)變量(屬性),對(duì)方法也一樣,調(diào)用的時(shí)候必須使用"類(lèi)名.屬性",不能使用"類(lèi)對(duì)象.屬性",因?yàn)樗鼘儆谝粋€(gè)類(lèi),而不是一個(gè)對(duì)象。
例如:
function User(name)
{
    this.name = name
}
User.type = "human"

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

另外,每個(gè)prototype都有一個(gè)constructor屬性,默認(rèn)用于保存constructor的定義,例如上面的user對(duì)象,調(diào)用:
user.constructor得到:
function User(name) { this.name = name; }
我們可以通過(guò)typeof,知道參數(shù)的類(lèi)型,假如是對(duì)象,就返回"object",假如是方法就返回"function"

6) 利用prototype實(shí)現(xiàn)類(lèi)間的繼承,例如:
// 父類(lèi)
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;
};

// 子類(lèi)
function ColorCircle(r,color){
    this.r = r;
    this.color = color;
}
ColorCircle.prototype = new Circle(0);  // 保存父類(lèi)的對(duì)象
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保存父類(lèi)的對(duì)象,在構(gòu)造子類(lèi)的時(shí)候,父類(lèi)對(duì)象同時(shí)被構(gòu)造(因?yàn)閜rototype被構(gòu)造)。也就是JavaScript繼承其實(shí)就是讓子類(lèi)的prototype對(duì)象保存父類(lèi)的對(duì)象。

相關(guān)文章

最新評(píng)論

浠水县| 阿图什市| 金湖县| 瓮安县| 唐海县| 万荣县| 绥滨县| 灯塔市| 丰宁| 若羌县| 外汇| 灵台县| 平遥县| 阿克陶县| 遵化市| 昌吉市| 科技| 兴国县| 清河县| 渝北区| 胶州市| 高淳县| 祁连县| 长寿区| 浑源县| 蓝田县| 凤庆县| 满洲里市| 大理市| 武宁县| 吕梁市| 遂平县| 图们市| 道孚县| 柳林县| 南宁市| 马边| 德庆县| 侯马市| 安乡县| 曲松县|