色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Javascript 原型和繼承(Prototypes and Inheritance)

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

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

prototype 對(duì)象和 constructor 相關(guān)聯(lián)。這意味著 prototype 可以作為放置方法以及其他常量的理想場(chǎng)所。原型中的屬性不會(huì)被復(fù)制到新創(chuàng)建的對(duì)象中去,他們的屬性看上去就像對(duì)象的屬性一樣。這意味著,使用原型能夠大幅度的減少多個(gè)同類對(duì)象占用的內(nèi)存。

每一個(gè) class 都只有一個(gè) prototype object, 附帶一套屬性。但我們?cè)谶\(yùn)行時(shí)可能會(huì)創(chuàng)建多個(gè)類的實(shí)例。那么如果發(fā)生對(duì)原型的屬性的讀寫會(huì)發(fā)生什么情況?
當(dāng)你讀一個(gè)屬性的時(shí)候,JavaScript 首先嘗試去查找對(duì)象本身是否有這個(gè)屬性,如果沒有,接著去查找原型里面是否有。有的話就返回結(jié)果。
而當(dāng)你寫原型的屬性的時(shí)候,因?yàn)槎鄠€(gè)對(duì)象共享原型,顯然是不能直接在原型上進(jìn)行寫操作的。這個(gè)時(shí)候?qū)嶋H上 JavaScript 會(huì)在對(duì)象上創(chuàng)建一個(gè)同名的屬性,然后把值寫到里面。當(dāng)你下次讀這個(gè)屬性的時(shí)候,JavaScript 一下子就在對(duì)象的屬性里查找到了,那么就不需要去原型里查找了。這個(gè)時(shí)候,我們說“對(duì)象的屬性掩蓋或隱藏了原型的屬性”。(shadows or hides) 。

從上面討論看出,其實(shí)我們?cè)谠O(shè)計(jì)類的時(shí)候,只要掌握一個(gè)原則:在原型里僅定義一些方法(方法一般是不會(huì)變的),常數(shù),常量等。做到這一點(diǎn)就不容易混淆了。
例子:

// 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(  );


內(nèi)置的類的 prototype.

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

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


然后我們就可以類似這樣的來調(diào)用了:

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

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

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 内江市| 扎兰屯市| 从江县| 淮南市| 如东县| 永济市| 赣榆县| 汶川县| 绩溪县| 奎屯市| 彭阳县| 贵南县| 新营市| 眉山市| 安徽省| 黄浦区| 通道| 绥宁县| 西城区| 秦安县| 商丘市| 岗巴县| 房产| 雷州市| 嘉义县| 石狮市| 霍邱县| 丽水市| 寻乌县| 临武县| 安泽县| 宁明县| 淮南市| 高碑店市| 灵台县| 奉化市| 白朗县| 余干县| 邻水| 虎林市| 南城县|