一篇文章帶你了解JavaScript對(duì)象原型
每一個(gè)JavaScript對(duì)象有一個(gè)原型,prototype也是一個(gè)對(duì)象。所有的JavaScript對(duì)象繼承的屬性和方法從它們的prototype。
一、JavaScript 原型
使用對(duì)象字面量創(chuàng)建對(duì)象,或者使用new Object(),從一個(gè)稱作Object.prototype的原型(prototype)繼承。使用 new Date()創(chuàng)建對(duì)象,繼承Date.prototype。
Object.prototype 是原型鏈的頂級(jí)原型。所有的JavaScript對(duì)象(Date, Array, RegExp, Function, ....) 都繼承Object.prototype。
1. 創(chuàng)建一個(gè)原型
創(chuàng)建對(duì)象原型的標(biāo)準(zhǔn)方法是使用對(duì)象構(gòu)造函數(shù):
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
使用構(gòu)造函數(shù),可以使用new關(guān)鍵字從同一原型創(chuàng)建新對(duì)象。
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sall", "Rally", 60, "green");
構(gòu)造函數(shù)是Person對(duì)象的原型,用大寫字母命名構(gòu)造函數(shù)是很好的做法。
完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>項(xiàng)目</title>
</head>
<body style="background-color: aqua;">
<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("Sall", "Rally", 60, "green");
document.getElementById("demo").innerHTML =
"My father is " + myFather.age + ". My mother is " + myMother.age;
</script>
</body>
</html>
2. 原型添加屬性
不能將新屬性添加到原型中,就像將新屬性添加到現(xiàn)有對(duì)象一樣,因?yàn)樵撛筒皇乾F(xiàn)有對(duì)象。
Person.nationality = "Chinese";
若要向原型添加新屬性,必須將其添加到構(gòu)造函數(shù):
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "Chinese";
}
原型屬性可以有原型值(默認(rèn)值)。
3. 為原型添加方法
構(gòu)造函數(shù)也可以定義方法:
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this.name = function() {
return this.firstName + " " + this.lastName
};
}
var myFather = new Person("John", "ele", 50, "blue");
document.getElementById("demo").innerHTML =
"My father is " + myFather.name();
</script>
二、向?qū)ο筇砑訉傩院头椒?/h3>
有時(shí),希望向現(xiàn)有對(duì)象添加新屬性,(或方法),希望將新屬性(或方法)添加到給定類型的所有現(xiàn)有對(duì)象中,您向?qū)ο笤吞砑有聦傩裕ɑ蚍椒ǎ?/p>
1. 向?qū)ο筇砑訉傩?/h4>
向現(xiàn)有對(duì)象添加新屬性很容易。
myFather.nationality = "English";
屬性將被添加到myFather,不是myMother,也不是任何其他person對(duì)象。
2. 向?qū)ο筇砑臃椒?/h4>
向現(xiàn)有對(duì)象添加新方法也很容易:
myFather.name = function () {
return this.firstName + " " + this.lastName;
};
方法將被添加到myFather。不是myMother。
三、使用 prototype 屬性
JavaScript prototype屬性允許你為一個(gè)已經(jīng)存在的原型添加新的屬性:
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Person.prototype.nationality = "Math";
var myFather = new Person("John", "Doe", 50, "blue");
document.getElementById("demo").innerHTML =
"My father is " + myFather.nationality;
</script>
JavaScript原型屬性還允許您添加新的方法對(duì)現(xiàn)有的原型:
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName
};
var myFather = new Person("name", "oe", 50, "blue");
document.getElementById("demo").innerHTML =
"My father is " + myFather.name();
</script>
只修改你設(shè)定的自己原型。不修改標(biāo)準(zhǔn)的JavaScript對(duì)象的原型。
四、總結(jié)
本文基于JavaScript基礎(chǔ)。介紹了JavaScript對(duì)象原型的基礎(chǔ)知識(shí)點(diǎn)。如何在原型的基礎(chǔ)上添加屬性和方法。如何在對(duì)象在添加屬性和方法。以及使用prototype屬性允許你為一個(gè)已經(jīng)存在的原型添加新的屬性。每個(gè)模塊都做了詳細(xì)講解,代碼的展示。
使用編程語(yǔ)言,希望能夠幫助你學(xué)習(xí)。