gpt4 book ai didi

javascript - 调用object.method.method,Javascript时出现问题

转载 作者:行者123 更新时间:2023-11-28 02:49:27 26 4
gpt4 key购买 nike

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

<!--
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>



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

而不是使用下面两个语句

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

它可以很好地产生上述输出。但我想使用嵌套方法生成上述输出。

我想使用单个语句 alert(lib.getBook("Dummy XML").getAuthors()); 来生成相同的输出( //输出 John Smith, Jack辛普森)

请帮助我了解如何在方法中调用方法。

谢谢

最佳答案

它的名字是 method chaining 。让函数返回 this。

   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") 返回时调用另一个方法....

关于javascript - 调用object.method.method,Javascript时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4102665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com