JavaScript是一種面向?qū)ο缶幊?OOP)語言。一種編程語言可以被稱為面向?qū)ο蟮?,它為開發(fā)者提供了四種基本功能:
封裝 - 存儲相關(guān)的信息,無論是數(shù)據(jù)或方法,還是對象
聚合 - 存儲一個對象到另一個對象的內(nèi)部
繼承 - 類的能力依賴于另一個類(或類數(shù)),用于其部分的屬性和方法
多態(tài)性 - 編寫函數(shù)或者方法,在各種不同的方式工作
對象是由屬性。如果屬性包含一個函數(shù),它被認為是一個對象的方法,否則,該屬性被認為是一個屬性。
對象的屬性可以是任何三種基本數(shù)據(jù)類型的,或者任何抽象數(shù)據(jù)類型,如另一個對象。對象屬性通常是內(nèi)部使用的對象的方法的變量,但也可以是用于整個頁面全局可見的變量。
用于添加屬性的目的語法是:
objectName.objectProperty = propertyValue;
下面是一個簡單的例子來說明如何利用“稱號”的文件對象的屬性來獲取文檔標題:
var str = document.title;
方法是讓對象做某件事。一個函數(shù)和一個方法,所不同的是一個 function語句的一個獨立的單元和方法被附加到對象,并可以通過這個關(guān)鍵字被引用之間的差別不大。
方法可用于一切從顯示對象的屏幕上的內(nèi)容,以對一組本地的屬性和參數(shù)執(zhí)行復雜的數(shù)學運算是有用的。
下面是一個簡單的例子來說明如何使用write()文檔對象的方法寫在文檔中的任何內(nèi)容:
document.write("This is test");
所有用戶定義的對象和內(nèi)置對象被稱為對象的對象的后代。
new運算符用于創(chuàng)建對象的實例。要創(chuàng)建一個對象,new運算符后面是構(gòu)造方法。
在下面的例子中,構(gòu)造方法Object(), Array(), 和 Date().。這些構(gòu)造函數(shù)是內(nèi)置的 JavaScript 函數(shù)。
var employee = new Object();
var books = new Array("C++", "Perl", "Java");
var day = new Date("August 15, 1947");
構(gòu)造函數(shù)是用來創(chuàng)建和初始化對象的函數(shù)。 JavaScript提供了一個特殊的構(gòu)造函數(shù)調(diào)用Object()來構(gòu)建的對象。Object()構(gòu)造的返回值被分配給一個變量。
變量包含一個引用到新的對象。分配給該對象的屬性是不變量,并且不使用var關(guān)鍵字來定義。
這個例子演示了如何創(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>
這個例子演示如何創(chuàng)建一個對象,一個用戶定義的函數(shù)。此處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)鍵字作為一種速記的引用對象的屬性或方法。
指定為參數(shù)的對象就成為接下來的塊的持續(xù)時間的默認對象。為對象的屬性和方法可以在不命名的對象。
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有幾個內(nèi)置的或本地對象。這些對象是在你的程序訪問的任何地方,將以同樣的方式工作在任何操作系統(tǒng)上運行的任何瀏覽器。
這里是所有重要的JavaScript本地對象的列表: