0

I have a syntax error that I don't know how to fix. This is the code:

function computer(speed, hdspace, ram)
{
    this.speed=speed;
    this.hdspace=hdspace;
    this.ram=ram;
    this.price=get_price();
}
function get_price()
{
    var the_price=500;
    the_price += (this.speed == "2GHz") ? 200 : 100;
    the_price += (this.hdspace == "80GB") ? 50 : 25;
    the_price += (this.ram == "1GB") ? 150 : 75;
    return the_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", "256");

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

document.write("<h1>Prices of the computers you requested:</h1>");
document.write("<h3><br/>Work Computer: </h3>"+work_computer);
document.write("Price: $"+work_computer_price);
document.write("<br/>");
document.write("Home Computer: "+home_computer);
document.write("Price: $"+home_computer_price);
document.write("<br/>");
document.write("Laptop Computer: "+laptop_computer);
document.write("Price: $"+laptop_computer_price);

On line 22, there is an error saying: Uncaught TypeError: Property 'price' of object # is not a function This is line 22:

var work_computer_price = work_computer.price();

Please help. Thanks!

1
  • Certainly not a SyntaxError, but a TypeError. Hint: Functions are invoked using (), and passed by reference when () is removed. /End of hint Commented Mar 24, 2012 at 21:52

3 Answers 3

2

Take away the parentheses when you assign this.price:

this.price=get_price;

You want to set the "price" property to reference the function itself, not the return value of calling it.

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

Comments

1

you better declare getPrice() as a member of the computer's prototype, like this:

var computer = function(speed, hdspace, ram)
{
    this.speed=speed;
    this.hdspace=hdspace;
    this.ram=ram;
    this.price=get_price();
}
computer.prototype = {
    get_price: function()
    {
        var the_price=500;
        the_price += (this.speed == "2GHz") ? 200 : 100;
        the_price += (this.hdspace == "80GB") ? 50 : 25;
        the_price += (this.ram == "1GB") ? 150 : 75;
        return the_price;
    }
}

3 Comments

This would work if it weren't for the syntax error :-) (The function needs to be declared get_price: function() { ... } )
thanks, i was in a rush and it slipt, just noticed and corrected.
If overwriting .prototype, make sure to restore .constructor.
0

how are you?

The problem is that price is not a function, its an attribute.

So basically:

var work_computer_price = work_computer.price;

will work.

Cheers!

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.