Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Quick Question on Assigning Method to Object

    function get_price()
    {
    var the_price=1000;
    if (this.speed=="500MGz")
    the_price+=200;
    else
    the_price+=100;
    if (this.hdspace=="15GB")
    the_price+=50;
    else
    the_price+=25;
    if (this.ram=="128MB")
    the_price+=150;
    else
    the_price+=75;
    return the_price;
    }

    function computer(speed,hdspace,ram)
    {
    this.speed=speed;
    this.hdspace=hdspace;
    this.ram=ram;
    this.price=get_price;
    }

    var work_computer=new computer("2GHz","80GB","1GB");
    var home_computer=new computer("1.5GHz","40GB","512MB");
    var laptop_computer=new computer("1GHz","20GB","256MB");

    var work_computer_price=work_computer.price();
    var home_computer_price=home_computer.price();
    var laptop_computer=laptop_computer.price();


    ___________________________________________________

    In the above code, the line I'm having trouble with is marked red.

    Why is it that when I call the method using ...

    this.price = get_price;

    It works fine.

    But if I assign the object this.price with like this...

    this.price = get_price();

    I get an error?

    Isn't get_price() a function? So shouldn't the parenthesis be included when the method is called? Why are we suppose to leave out the parenthesis?! I'm confused.

    Thanks!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You are *NOT* "calling" the function when you do this.price = get_price;

    You are, for all intents and purpose, making this.price an *ALIAS* for get_price.

    That is, you have assigned a *REFERENCE* to the function to the member varialbe price. So that later you could do (as you are indeed doing) work_computer.price();

    The reason that doing this.price = get_price() barfs on your feet is that then you *ARE* invoking the function. But then when you get into the function, the keyword this has NO REFERENCE object to refer to!!! That is, at that point this is either null or maybe refers to the whole document.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Ahlahn (01-08-2011)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,190
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Code:
    this.price = get_price.call(this);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I was going to do it uglier, but yeah...why not.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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