SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Object-Oriented Programming Part 2

    Hi,
    I was going through the LIBRARY example described in js prog part2,Here is my doubt:

    1)How is the "person" object refering to "author" as author is defined in the "book" constructor.
    2)How is "books" property in library constructor accessing "price" property defined in "book" class.


    Regards
    venkatesh

  2. #2
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    one more doubt

    this.totalPrice = function(),is defined outside the library constructor, so to which object is "this" referring to?

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you provide a link to the article you're referring to?

  4. #4

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Raffles.
    1)How is the "person" object refering to "author" as author is defined in the "book" constructor.
    A "person" object is not "refering" to the "author". Instead, the "author" is an instance of a "person". As per that article:
    Note that we're passing instances of the Person object as the last arguments to create the authors property of the Book object.
    So, for example, you could even do something like this:
    Code JavaScript:
    var myBook = new Book( "My Example", 120, 19.99, new Person("Shmoe", "Joe"));
    or
    Code JavaScript:
    var joeShmoe = new Person("Shmoe", "Joe");
    var myBook = new Book( "My Example", 120, 19.99, joeShmoe);
    2)How is "books" property in library constructor accessing "price" property defined in "book" class.
    Well, the Library object contains an array of Book objects. The Book objects are passed to the Library constructor, so it populates this array when the constructor is called. For example:

    Code JavaScript:
    // Create 2 books by author "Joe Shmoe"
    var joeShmoe = new Person("Shmoe", "Joe");
    var myBook = new Book("My Example", 120, 19.99, joeShmoe);
    var yourBook = new Book("My Example: The Sequel", 114, 19.99, joeShmoe);
     
    // Create the Library containing all of the books
    var myLibrary = new Library(myBook, yourBook);

    The Library constructor contains a function totalPrice:
    Code JavaScript:
    this.totalPrice = function() {
        var totalCost = 0;
        for(i=0;i<this.books.length;i++) {
            totalCost += this.books[i].price;
        }
        return totalCost;
    }
    What that does is loops through the Library's array of Book objects (this.books). It access each of the books in the array (this.books[i]) and access the price property of the Book class (.price).

    this.totalPrice = function(),is defined outside the library constructor, so to which object is "this" referring to?
    No, it's not. It is defined within the constructor. Here's a snippet of the code example (I've formatted it slightly so you can see that it's within the Library):
    Code JavaScript:
    //define our Library() constructor
    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 = new Number(0);
            for(i=0;i<this.books.length;i++){
                totalCost += this.books[i].price;
            }
            return totalCost;
        } 
        // SNIPPING THE REST
    }

  6. #6
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks very much, for explainig in detail.

    Thanks & Regards
    venkatesh


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •