JavaScript 是一門支援物件導向程式設計(OOP)的語言。要被稱為物件導向,一門語言通常需要提供以下四項基本能力:
物件由屬性構成。如果某個屬性的值是一個函式,我們稱它為物件的「方法」;否則就是一般的「屬性」。
物件的屬性可以是三種基本資料型別之一,也可以是抽象資料型別(例如另一個物件)。屬性通常是供物件方法內部使用的變數,但也可以用來存放整個頁面都能存取的變數。
新增屬性的語法如下:
objectName.objectProperty = propertyValue;
以下範例示範如何取得文件物件的標題屬性:
var str = document.title;
方法讓物件得以「做某件事」。函式與方法之間的差別在於:函式是一個獨立的程式碼單元,而方法則是附加在物件上、可透過 this 關鍵字參照的函式。方法可以用來把內容輸出到畫面,也能對一組本地屬性與參數進行複雜的運算。
以下範例示範使用 write() 方法把內容寫入文件:
document.write("This is test");所有使用者自訂物件與內建物件,都是 Object 物件的後代。
new 運算子用來建立物件的實例,它後面接著一個建構方法(constructor)。以下範例使用內建的 Object()、Array() 與 Date() 建構函式:
var employee = new Object();
var books = new Array("C++", "Perl", "Java");
var day = new Date("August 15, 1947");建構函式是用來建立並初始化物件的函式。JavaScript 提供了 Object() 這個特殊建構函式來建立物件,其回傳值會被指派給一個變數,該變數便指向這個新物件。指派給物件的屬性無須使用 var 關鍵字宣告。
範例一:如何建立一個物件
<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>範例二:透過使用者自訂函式建立物件,此處以 this 關鍵字指向傳入函式的物件:
<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>前面的範例說明了如何用建構函式建立物件並指派屬性,但要完整定義一個物件,還需要為它加上方法。
<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 關鍵字可作為一種簡寫,用來簡化對物件屬性或方法的參照。指定給它的物件會成為後續程式碼區塊的預設物件,讓你在使用屬性與方法時不必一再寫出物件名稱。
語法如下:
with (object){
properties used without the object name and dot
}<html>
<head>
<title>User-defined objects</title>
<script type="text/javascript">
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 中建立並操作自己的物件了。