自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇文章帶你了解JavaScript對(duì)象原型

開發(fā) 前端
每一個(gè)JavaScript對(duì)象有一個(gè)原型,prototype也是一個(gè)對(duì)象。所有的JavaScript對(duì)象繼承的屬性和方法從它們的prototype。

每一個(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í)。

責(zé)任編輯:華軒 來(lái)源: 前端進(jìn)階學(xué)習(xí)交流
相關(guān)推薦

2023-08-27 15:18:17

JavaScriptRegExp

2024-07-02 14:14:18

2023-07-25 16:06:57

JavaScript對(duì)象

2021-07-02 10:00:50

JavaScriptObject 函數(shù)

2020-10-22 09:08:34

JavaScript

2023-07-06 14:40:38

2024-09-02 14:07:05

2020-11-10 10:48:10

JavaScript屬性對(duì)象

2021-01-29 18:41:16

JavaScript函數(shù)語(yǔ)法

2021-02-02 18:39:05

JavaScript

2021-06-04 09:56:01

JavaScript 前端switch

2023-07-30 15:18:54

JavaScript屬性

2021-01-26 23:46:32

JavaScript數(shù)據(jù)結(jié)構(gòu)前端

2021-03-09 14:04:01

JavaScriptCookie數(shù)據(jù)

2021-06-24 09:05:08

JavaScript日期前端

2023-09-06 14:57:46

JavaScript編程語(yǔ)言

2024-01-30 13:47:45

2024-04-19 14:23:52

SwitchJavaScript開發(fā)

2021-03-05 18:04:15

JavaScript循環(huán)代碼

2021-05-18 08:30:42

JavaScript 前端JavaScript時(shí)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)