溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JavaScript prototype原型用法

發(fā)布時(shí)間:2020-07-05 08:52:10 來(lái)源:網(wǎng)絡(luò) 閱讀:349 作者:web全棧 欄目:web開(kāi)發(fā)

JavaScript對(duì)象原型

所有JavaScript對(duì)象都從原型繼承屬性和方法。

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>js</title>
<body>

<h3>JavaScript 對(duì)象</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>

我們還了解到,您無(wú)法向現(xiàn)有對(duì)象構(gòu)造函數(shù)添加新屬性:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript對(duì)象</title>
<body>

<h3>JavaScript對(duì)象</h3>

<p>您無(wú)法向構(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對(duì)象</title>
<body>

<h3> JavaScript對(duì)象</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 =
        "我父親的國(guó)籍是 " + myFather.nationality + ". 我母親的國(guó)籍是: " + myMother.nationality;
</script>

</body>
</html>
  • 原型繼承

    所有JavaScript對(duì)象都從原型繼承屬性和方法:

    Object.prototype位于原型繼承鏈的頂部:Date對(duì)象,Array對(duì)象和Person對(duì)象繼承自O(shè)bject.prototype。

    • Date 對(duì)象繼承自 Date.prototype
    • Array 對(duì)象繼承自 Array.prototype
    • Person 對(duì)象繼承自 Person.prototype

向?qū)ο筇砑訉傩院头椒?/h2>

有時(shí),您希望向給定類(lèi)型的所有現(xiàn)有對(duì)象添加新屬性(或方法)。有時(shí)您想要向?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;
};

更好的原型對(duì)象的文章

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI