2010-11-05 40 views
1
////////////////////////////////////////////////////////////////////////////// 

<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<script type="text/javascript"> 
// 3 constructor functions of Person, Book, and Library 
function Person(fname,lname) 
{ 
    this.firstName = fname; 
    this.lastName = lname; 
} 

function Book(booktitle,pages,price) 
{ 
    this.bookTitle = booktitle; 
    this.pages = pages; 
    this.price = price; 
    this.authors = new Array(arguments.length-3); 
    for(i=0;i<arguments.length-3;i++) 
    { 
    this.authors[i] = arguments[i+3]; 
    } 
} 

function Library() 
{ 
    this.books = new Array(arguments.length); 
    for(i=0;i<arguments.length;i++) 
    { 
    this.books[i] = arguments[i]; 
    } 
    this.totalPrice = function(){ 
    var totalCost = 0; 
    for(i=0;i<this.books.length;i++) 
    { 
    totalCost += this.books[i].price; 
    } 
    return totalCost; 
    } 
    this.averagePrice = new Function("return this.totalPrice()/this.books.length"); 
    var flag; 
    this.getBook = function(name){ 
     for(i=0;i<this.books.length;i++) 
    { 
     if(this.books[i].bookTitle == name) 
     { 
     this.flag = i; 
     } 
    } 


    } 

    this.getAuthors = function(){ 
    var toSay = ""; 

    for(j=0;j<this.books[this.flag].authors.length;j++){ 
     var authName = 
     this.books[this.flag].authors[j].lastName + " " + 
     this.books[this.flag].authors[j].firstName + "\t"; 
     if(toSay.indexOf(authName)!=-1) 
     continue; 
     toSay+=""+authName; 
    } 

return toSay; 
} 
} 


var john = new Person("Smith", "John"); 
var jack = new Person("Simpson", "Jack"); 
var bobby = new Person("Franklin", "Bobby"); 
var albert = new Person("Camus", "Albert"); 
var java = new Book("Dummy Java", 1000, 29.95, john, jack); 
var php = new Book("Dummy PHP", 300, 19.95, john); 
var xml = new Book("Dummy XML", 150, 9.95, bobby, albert); 
var js = new Book("Dummy JavaScript", 2000, 49.95, albert); 
var lib = new Library(java, php, xml, js); 
alert(lib.totalPrice()); // output 109.8 
alert(lib.averagePrice()); // output 27.45 
lib.getBook("Dummy XML"); 
alert(lib.getAuthors()); // output John Smith, Jack Simpson 
</script> 
</head> 
<body> 
</body> 
</html> 



///////////////////////////////////////////////////////////////////// 

而不是使用下面的兩個語句問題,呼籲object.method.method,使用Javascript

lib.getBook("Dummy XML"); 
alert(lib.getAuthors()); // output John Smith, Jack Simpson 

它正常工作產生上面的輸出。但我想使用嵌套方法生成abouve輸出。

我想用一個單獨的語句alert(lib.getBook("Dummy XML").getAuthors());產生相同的輸出(// output John Smith, Jack Simpson

請幫我如何調用一個方法的方法。

感謝

回答

1

它被稱爲method chaining。讓函數返回這個。

this.getBook = function(name){ 
     for(i=0;i<this.books.length;i++) { 
      if(this.books[i].bookTitle == name)   { 
       this.flag = i; 
      } 
     } 

     return this; 
    } 

,那麼你可以調用的lib.getBook("Dummy XML")回報....

+0

.Wow另一種方法!這就像一個魅力,我是一個JS的新手,感謝您告訴我方法鏈,感謝您的幫助。 – 2010-11-05 01:00:49

0

你需要重新定義getBook()函數,以便你的想法工作回到「LIB」的參考。