所有JavaScript對象都從原型繼承屬性和方法。
10年積累的網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有安寧免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>js</title>
<body>
<h3>JavaScript 對象</h3>
<p id="demo"></p>
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");
document.getElementById("demo").innerHTML =
"My father is " + myFather.age + ". My mother is " + myMother.age;
</script>
</body>
</html>
我們還了解到,您無法向現(xiàn)有對象構(gòu)造函數(shù)添加新屬性:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript對象</title>
<body>
<h3>JavaScript對象</h3>
<p>您無法向構(gòu)造函數(shù)添加新屬性。</p>
<p id="demo"></p>
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Person.nationality = "English";
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");
document.getElementById("demo").innerHTML =
"The nationality of my father is " + myFather.nationality;
</script>
</body>
</html>
要向構(gòu)造函數(shù)添加新屬性,必須將其添加到構(gòu)造函數(shù):
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript對象</title>
<body>
<h3> JavaScript對象</h3>
<p id="demo"></p>
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this.nationality = "English";
}
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");
document.getElementById("demo").innerHTML =
"我父親的國籍是 " + myFather.nationality + ". 我母親的國籍是: " + myMother.nationality;
</script>
</body>
</html>
所有JavaScript對象都從原型繼承屬性和方法:
Object.prototype位于原型繼承鏈的頂部:Date對象,Array對象和Person對象繼承自O(shè)bject.prototype。
有時,您希望向給定類型的所有現(xiàn)有對象添加新屬性(或方法)。有時您想要向?qū)ο髽?gòu)造函數(shù)添加新屬性(或方法)。
JavaScript prototype屬性允許您向?qū)ο髽?gòu)造函數(shù)添加新屬性:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
JavaScript prototype屬性還允許您向?qū)ο髽?gòu)造函數(shù)添加新方法:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
更好的原型對象的文章
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:JavaScriptprototype原型用法-創(chuàng)新互聯(lián)
新聞來源:http://aaarwkj.com/article26/dgdcjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、用戶體驗、微信小程序、關(guān)鍵詞優(yōu)化、云服務(wù)器、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容