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>



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

Instead of using the below two statements

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

it works fine to produce the above output. but i want to produce the abouve output using nested methods.

i want to use a single statement alert(lib.getBook("Dummy XML").getAuthors()); to produce the same output ( // output John Smith, Jack Simpson)

Please help me on how to call a method in a method.

Thanks

2 Answers 2

1

It's called method chaining. Have the functions return this.

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

        return this;
   }

then you can call another method on the return of lib.getBook("Dummy XML")....

Sign up to request clarification or add additional context in comments.

1 Comment

.Wow! That works like a charm, i am a rookie in JS, Thanks for leting me know about method chaining , Thanks for you help.
0

You'll need to redefine the getBook() function to return a reference to 'lib' in order for your idea to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.