欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解JavaScript中的對象

 更新時間:2015年06月04日 11:34:09   投稿:goldensun  
這篇文章主要介紹了深入理解JavaScript中的對象,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下

 JavaScript是一種面向?qū)ο缶幊?OOP)語言。一種編程語言可以被稱為面向?qū)ο蟮模鼮殚_發(fā)者提供了四種基本功能:

  •     封裝 - 存儲相關(guān)的信息,無論是數(shù)據(jù)或方法,還是對象
  •     聚合 -  存儲一個對象到另一個對象的內(nèi)部
  •     繼承 - 類的能力依賴于另一個類(或類數(shù)),用于其部分的屬性和方法
  •     多態(tài)性 - 編寫函數(shù)或者方法,在各種不同的方式工作

對象是由屬性。如果屬性包含一個函數(shù),它被認(rèn)為是一個對象的方法,否則,該屬性被認(rèn)為是一個屬性。
對象屬性:

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

用于添加屬性的目的語法是:

objectName.objectProperty = propertyValue;

示例 :

下面是一個簡單的例子來說明如何利用“稱號”的文件對象的屬性來獲取文檔標(biāo)題:

var str = document.title;


對象的方法:

方法是讓對象做某件事。一個函數(shù)和一個方法,所不同的是一個 function語句的一個獨立的單元和方法被附加到對象,并可以通過這個關(guān)鍵字被引用之間的差別不大。

方法可用于一切從顯示對象的屏幕上的內(nèi)容,以對一組本地的屬性和參數(shù)執(zhí)行復(fù)雜的數(shù)學(xué)運算是有用的。
例子:

下面是一個簡單的例子來說明如何使用write()文檔對象的方法寫在文檔中的任何內(nèi)容:

document.write("This is test");


用戶定義的對象:

所有用戶定義的對象和內(nèi)置對象被稱為對象的對象的后代。
new 操作符:

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");


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

構(gòu)造函數(shù)是用來創(chuàng)建和初始化對象的函數(shù)。 JavaScript提供了一個特殊的構(gòu)造函數(shù)調(diào)用Object()來構(gòu)建的對象。Object()構(gòu)造的返回值被分配給一個變量。

變量包含一個引用到新的對象。分配給該對象的屬性是不變量,并且不使用var關(guān)鍵字來定義。
示例 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:

這個例子演示如何創(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)鍵字:

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

指定為參數(shù)的對象就成為接下來的塊的持續(xù)時間的默認(rèn)對象。為對象的屬性和方法可以在不命名的對象。
語法

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>

相關(guān)文章

最新評論