JavaScript 對象概述

2018-09-27 17:45 更新

對象概述

JavaScript 是一種面向?qū)ο缶幊陶Z言 ( OOP ) 。一種語言如果可以為開發(fā)者提供四種基本功能就可以被稱為面向?qū)ο?

  • 封裝:把相關(guān)信息,無論數(shù)據(jù)或方法都存儲在一個對象中的能力。
  • 聚合:將一個對象存儲在另一個對象中的能力。
  • 繼承:一個類依賴另一個類 ( 或一些類 )中的一些性質(zhì)和方法的能力。
  • 多態(tài):寫一個函數(shù)或方法,這個函數(shù)或方法可以以各種不同形式工作的能力。

對象由屬性組成。如果一個屬性包含一個函數(shù),它被認為是這個對象的一個方法,否則這個屬性被認為成一個屬性。

對象的屬性

對象屬性可以是任何三個基本數(shù)據(jù)類型,或抽象數(shù)據(jù)類型中的任何一個,如另一個對象。對象屬性通常是在對象的方法內(nèi)部使用的變量,也可以是在整個頁面中使用的全局變量。

將屬性添加到對象的語法是:

objectName.objectProperty = propertyValue; 

示例

以下是一個簡單的例子,介紹了如何使用文檔對象的 “title” 屬性來獲得一個文檔標題:

var str = document.title;

對象的方法

方法是函數(shù)讓對象做某事或讓某事作用在這個對象上。一個函數(shù)和一個方法之間,除了函數(shù)是聲明的一個獨立的單元,而方法是附加到某個對象上并且可以使用 this 關(guān)鍵字引用方法,除此之外幾乎沒有差別。

方法從將對象的內(nèi)容顯示到屏幕上到對一組本地屬性和參數(shù)執(zhí)行復(fù)雜的數(shù)學(xué)運算都很有用。

示例

以下是一個簡單的例子,介紹了怎樣使用文檔對象的 write() 方法在文檔中寫任何內(nèi)容:

document.write("This is test");

用戶定義的對象

所有用戶定義的對象和對象中的內(nèi)置對象都是一個被稱為 Object 的對象的后代。

new 運算符

new 運算符用于創(chuàng)建對象的實例。若要創(chuàng)建一個對象,new 運算符后緊接著是構(gòu)造函數(shù)方法。

在以下示例中,構(gòu)造函數(shù)方法是 Object() , Array() ,和 Date() 。這些函數(shù)是內(nèi)置的 JavaScript 函數(shù)。

var employee = new Object();  
var books = new Array("C++","Perl","Java");  
var day = new Date("August 15,1947");   

Object() 構(gòu)造函數(shù)

構(gòu)造函數(shù)是一個可以創(chuàng)建和初始化對象的函數(shù)。 JavaScript 提供了名為Object() 的一個特殊的構(gòu)造函數(shù)來生成對象。 Object() 構(gòu)造函數(shù)將返回值賦給一個變量。

示例 1

此示例演示如何創(chuàng)建一個對象:

    <html>
    <head>
    <title>User-defined objects</title>
    <script type="text/javascript">
    var book = new Object();   // Create the object
        book.subject = "Perl"; // Assign properties to the object
        book.author  = "Mohtashim";
    </script>
    </head>
    <body>
    <script type="text/javascript">
       document.write("Book name is : " + book.subject + "<br>");
       document.write("Book author is : " + book.author + "<br>");
    </script>
    </body>
    </html>

示例 2

此示例演示了如何用用戶定義的函數(shù)來創(chuàng)建一個對象。這里的 this 關(guān)鍵字用于引用被傳遞給函數(shù)的對象。

    <html>
    <head>
    <title>User-defined objects</title>
    <script type="text/javascript">
    function book(title, author){
        this.title = title; 
        this.author  = author;
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
       var myBook = new book("Perl", "Mohtashim");
       document.write("Book title is : " + myBook.title + "<br>");
       document.write("Book author is : " + myBook.author + "<br>");
    </script>
    </body>
    </html>

為對象定義的方法

前面的示例演示了如何用構(gòu)造函數(shù)創(chuàng)建對象和分配屬性。但是我們需要通過給對象分配方法來完成一個對象的定義。

示例

這里是一個簡單的示例,說明了如何與一個對象一起添加一個函數(shù)。

    <html>
    <head>
    <title>User-defined objects</title>
    <script type="text/javascript">

    // Define a function which will work as a method
    function addPrice(amount){
        this.price = amount; 
    }

    function book(title, author){
        this.title = title; 
        this.author  = author;
        this.addPrice = addPrice; // Assign that method as property.
    }

    </script>
    </head>
    <body>
    <script type="text/javascript">
       var myBook = new book("Perl", "Mohtashim");
       myBook.addPrice(100);
       document.write("Book title is : " + myBook.title + "<br>");
       document.write("Book author is : " + myBook.author + "<br>");
       document.write("Book price is : " + myBook.price + "<br>");
    </script>
    </body>
    </html>

with 關(guān)鍵字

with 關(guān)鍵字被用來作為用于引用一個對象的屬性或方法的一種速記。

對象被指定成 with 關(guān)鍵字的參數(shù),進而成為后面語句塊的默認對象。這個對象的下的屬性和方法可以不指定對象名而直接使用。

語法

     with (object){
    properties used without the object name and dot
    }    

示例

    <html>
    <head>
    <title>User-defined objects</title>
    <script type="text/javascript">

    // Define a function which will work as a method
    function addPrice(amount){
    with(this){
       price = amount; 
    }
    }
    function book(title, author){
        this.title = title; 
        this.author  = author;
        this.price = 0;
        this.addPrice = addPrice; // Assign that method as property.
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
       var myBook = new book("Perl", "Mohtashim");
       myBook.addPrice(100);
       document.write("Book title is : " + myBook.title + "<br>");
       document.write("Book author is : " + myBook.author + "<br>");
       document.write("Book price is : " + myBook.price + "<br>");
    </script>
    </body>
    </html>    

JavaScript Native 對象

JavaScript 有幾個內(nèi)置或 native 對象。這些對象可以在任何地方被訪問,并且在任何瀏覽器中運行的任何操作系統(tǒng)的工作方式相同。

這里是所有重要的 JavaScript native 對象的列表:


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號