...

View Full Version : Unexpected Code Problems:



lgm432
10-05-2008, 07:55 PM
I have been reading through a book on javascript and I am trying to use a code I found in it.

This is what I have in the head.



<script language="JavaScript">
<!--
function get_price()
{
var the_price=1000;
if (this.speed == '500 MHz')
the_price+=100;
else
the_price+=200;
if (this.hdspace == '15 GB')
the_price+=50;
else
the_price+=25;
if (this.ram == '128 MB')
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('2 GHz' , '80 GB' , 1 GB');
var home_computer= new computer('1.5 GHz' , '40 GB' , '512 MB');
var laptop_computer= new computer('1 GHz' , '20 GB' , '256 MB');

var work_computer_price= work_computer.price();
var home_computer_price= home_computer.price();
var laptop_computer_price= laptop_computer.price();
//-->
</script>


In the body I have a bunch of document.write with the variables located inside. The book was written in 2004 so is it a problem with the edition of the Javascript code or just a mistake I have made?

Arty Effem
10-05-2008, 08:04 PM
function computer(speed,hdspace,ram)
{
this.speed=speed;
this.hdspace=hdspace;
this.ram=ram;
this.price=get_price;
}That should be a function call, not a function address:this.price=get_price();



var work_computer_price= work_computer.price();
var home_computer_price= home_computer.price();
var laptop_computer_price= laptop_computer.price();These are calls to a member function of computer but get_price isn't a member of computer, so the listing is inconsistent.

Declare get_price like this:

computer.prototype.get_price=function() and in the constructor call it thus:
this.price=this.get_price();

lgm432
10-05-2008, 08:28 PM
Thank you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum