在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 對象概述
頁面打印
事件
JavaScript 內(nèi)置函數(shù)
For 循環(huán)
對象概述
正則表達式
函數(shù)
算數(shù)對象
For...in
位置結(jié)構(gòu)
瀏覽器兼容性
數(shù)字對象
錯誤 & 異常處理
啟用
文檔對象模型
動畫
循環(huán)控制
While 循環(huán)
If...Else
頁面重定向
概述
語法
多媒體
對話框
快速指南
日期對象
Cookies
調(diào)試
表單有效性驗證
布爾對象
Void 關(guān)鍵字
數(shù)組對象
變量
運算符
Switch Case
圖像映射
字符串對象

對象概述

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 對象的列表:

上一篇:文檔對象模型下一篇:If...Else