天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

Javascript 原型和繼承(Prototypes and Inheritance)

JavaScript 對象從一個原形對象(prototype object) 繼承屬性。所有對象都有原型;原型的所有屬性看上去就像使用它作為原型的那些對象的屬性一樣。簡單的說就是:所有對象都從他的原型繼承屬性
(each object inherits properties from its prototype).

對象的 prototype 通過它的 constructor function 來定義。JavaScript 里所有的 function 都有一個 prototype 屬性。這個屬性開始是空的,接下來你給他添加的任何屬性都會被 constructor 創建的對象所擁有。

prototype 對象和 constructor 相關聯。這意味著 prototype 可以作為放置方法以及其他常量的理想場所。原型中的屬性不會被復制到新創建的對象中去,他們的屬性看上去就像對象的屬性一樣。這意味著,使用原型能夠大幅度的減少多個同類對象占用的內存。

每一個 class 都只有一個 prototype object, 附帶一套屬性。但我們在運行時可能會創建多個類的實例。那么如果發生對原型的屬性的讀寫會發生什么情況?
當你讀一個屬性的時候,JavaScript 首先嘗試去查找對象本身是否有這個屬性,如果沒有,接著去查找原型里面是否有。有的話就返回結果。
而當你寫原型的屬性的時候,因為多個對象共享原型,顯然是不能直接在原型上進行寫操作的。這個時候實際上 JavaScript 會在對象上創建一個同名的屬性,然后把值寫到里面。當你下次讀這個屬性的時候,JavaScript 一下子就在對象的屬性里查找到了,那么就不需要去原型里查找了。這個時候,我們說“對象的屬性掩蓋或隱藏了原型的屬性”。(shadows or hides) 。

從上面討論看出,其實我們在設計類的時候,只要掌握一個原則:在原型里僅定義一些方法(方法一般是不會變的),常數,常量等。做到這一點就不容易混淆了。
例子:

// Define a constructor method for our class.
// Use it to initialize properties that will be different for
// each individual Circle object.
functionCircle(x, y, r)
{
    this.x = x;  // The X-coordinate of the center of the circle

this.y = y;  // The Y-coordinate of the center of the circle

this.r = r;  // The radius of the circle
}


// Create and discard an initial Circle object.
// This forces the prototype object to be created in JavaScript 1.1.
new Circle(0,0,0);


// Define a constant: a property that will be shared by
// all circle objects. Actually, we could just use Math.PI,
// but we do it this way for the sake of instruction.
Circle.prototype.pi =
3.14159;


// Define a method to compute the circumference of the circle.
// First declare a function, then assign it to a prototype property.
// Note the use of the constant defined above.
function Circle_circumference(  ) { return
2
*
this.pi *
this.r; }
Circle.prototype.circumference =Circle_circumference;


// Define another method. This time we use a function literal to define
// the function and assign it to a prototype property all in one step.
Circle.prototype.area =
function(  ) { return
this.pi *
this.r *
this.r; }


// The Circle class is defined.
// Now we can create an instance and invoke its methods.
var c =
new Circle(0.0, 0.0, 1.0);
var a =c.area(  );
var p = c.circumference(  );


內置的類的 prototype.

不光是用戶自定義的類可以有 prototype. 系統內置的類比如 String, Date 也都有的。而且你可以向他們添加新的方法,屬性等。
下面這段代碼就對所有的 String 對象添加了一個有用的函數:

// Returns true if the last character is c
String.prototype.endsWith =
function(c) {
    return (c ==
this.charAt(this.length-1))
}


然后我們就可以類似這樣的來調用了:

var message =
"hello world";
message.endsWith('h')  // Returns false
message.endsWith('d')  // Returns true

JavaScript技術Javascript 原型和繼承(Prototypes and Inheritance),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 轻点慢点1V2啊高H抽插 | 国产精品AV无码免费播放 | 国产成人免费高清视频 | 国内高清在线观看视频 | 亚洲国产欧美另类 | 亚洲成人三级 | 大地影院在线播放 | 一本道久在线综合色姐 | 欧美91精品久久久久网免费 | 丰满的女朋友 在线播放 | 农村脱精光一级 | 神马影院午夜伦理限级 | X8X8拨牐拨牐X8免费视频8午夜 | 日日摸夜夜嗷嗷叫日日拍 | 午夜向日葵高清在线观看 | 98国产精品人妻无码免费 | 无码任你躁久久久久久老妇双奶 | 欧美97色伦影院在线观看 | 91香蕉福利一区二区三区 | 三级中国免费的 | 99这里只有是精品2 99这里有精品视频视频 | 毛片视频大全 | 岛国精品在线观看 | 无颜之月5集全免费看无删除 | 亚洲另类欧美综合在线 | 香蕉久久夜色精品国产小优 | 芭乐草莓樱桃丝瓜18岁大全 | 国产精品美女久久久久浪潮AV | 久久er99热精品一区二区 | 秋霞午夜鲁丝片午夜精品久 | 97视频免费上传播放 | 亚洲国产精品天堂在线播放 | 久久日韩精品无码一区 | 亚洲永久免费视频 | 帅哥男男GV在线1080P | 快播看av | 精品国产乱码久久久久久免费 | 亚洲精品无码不卡在线播HE | 啪啪做羞羞事小黄文 | 97免费观看视频 | 高肉黄暴NP文公交车 |