10 Replies - 1100 Views - Last Post: 09 May 2014 - 11:29 PM

#1 brian28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-March 14

basic basic javascript question

Posted 07 May 2014 - 01:37 PM

hello, I can't find whats wrong with my .js . It comes up....

"" Work Computer: 2GHZ, 80GB, 1GB
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; } ""

....on the webpage. I pressed F12 and i seen it said " $function get_price() but i don't know what it means, would appreciate the help.

<html>
<body>
<script type="text/javascript"src="prjs8_1.js"></script>
</body>




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;
}

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_price = laptop_computer.price();

document.write("<h2>The information on the computers you requested:</h2>");
document.write("<strong>Work Computer: </strong>");
document.write(work_computer.speed+", "+work_computer.hdspace+", "+work_computer.ram);
document.write("<br />");
document.write("<strong>Price:</strong> $"+work_computer.price);
document.write("<p>");
document.write("<strong >Home Computer: </strong>");
document.write(home_computer.speed+", "+home_computer.hdspace+","+home_computer.ram);
document.write("<br />");
document.write("<strong> Price: </strong> $"+home_computer.price);
document.write("</p>");
document.write("<p>");
document.write("<strong>Laptop:</strong");
document.write(laptop_computer.speed+", "+laptop_computer.hdspace+", "+laptop_computer.ram);
document.write("<br />");
document.write("<strong>Price:</strong> $"+laptop_computer.price);
document.write("</p>");



Mod edit - fixed code tags.

Is This A Good Question/Topic? 0
  • +

Replies To: basic basic javascript question

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2063
  • View blog
  • Posts: 6,327
  • Joined: 15-January 14

Re: basic basic javascript question

Posted 07 May 2014 - 01:51 PM

You need to actually call the function:

document.write("<strong>Price:</strong> $"+work_computer.price());
Was This Post Helpful? 0
  • +
  • -

#3 Blindman67   User is offline

  • D.I.C Addict
  • member icon

Reputation: 140
  • View blog
  • Posts: 620
  • Joined: 15-March 14

Re: basic basic javascript question

Posted 07 May 2014 - 03:58 PM

What you are seeing is the result of the code at line 15.
this.price = get_price;

At line 1 you have a function that is named get_price so on line 15 you are assigning this.price the contents of get_price which is the source code of the function. That is obviously not what you want.

To run/call a function javascript requires special notation. The function name followed by closed brackets "()"

So to make your program work you should change line 15 to
this.price = get_price(); // Notice the () telling the browser
                                // to run the code "get_price"

The "()" instruct the browser that you want to run there code in the function get_price.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2063
  • View blog
  • Posts: 6,327
  • Joined: 15-January 14

Re: basic basic javascript question

Posted 08 May 2014 - 09:43 AM

Quote

At line 1 you have a function that is named get_price so on line 15 you are assigning this.price the contents of get_price which is the source code of the function. That is obviously not what you want.

No, that's correct. He is assigning a function as a method of the object. This is the problematic line where he is referring to the method but not calling it:

document.write("<strong>Price:</strong> $"+work_computer.price);



He's doing it correctly here, but then he doesn't use that variable anywhere else:

var work_computer_price = work_computer.price();



It's not going to work to do what you suggest because get_price is using this, which is not going to be set to the object he wants if he calls it like you're suggesting. This is the alternative:

this.price = get_price.call(this);

This post has been edited by ArtificialSoldier: 08 May 2014 - 09:44 AM

Was This Post Helpful? 1
  • +
  • -

#5 Blindman67   User is offline

  • D.I.C Addict
  • member icon

Reputation: 140
  • View blog
  • Posts: 620
  • Joined: 15-March 14

Re: basic basic javascript question

Posted 08 May 2014 - 09:59 AM

Oops my bad. Sorry OP I should have looked deeper.

This post has been edited by Blindman67: 08 May 2014 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 Blindman67   User is offline

  • D.I.C Addict
  • member icon

Reputation: 140
  • View blog
  • Posts: 620
  • Joined: 15-March 14

Re: basic basic javascript question

Posted 08 May 2014 - 10:19 AM

He can do that but he has to then get rid of line 22-24 as he is calling price again there and now it is a number..
Was This Post Helpful? 0
  • +
  • -

#7 brian28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-March 14

Re: basic basic javascript question

Posted 08 May 2014 - 08:52 PM

thank you i appreciate the help

View PostArtificialSoldier, on 08 May 2014 - 09:43 AM, said:

Quote

At line 1 you have a function that is named get_price so on line 15 you are assigning this.price the contents of get_price which is the source code of the function. That is obviously not what you want.

No, that's correct. He is assigning a function as a method of the object. This is the problematic line where he is referring to the method but not calling it:

document.write("<strong>Price:</strong> $"+work_computer.price);



He's doing it correctly here, but then he doesn't use that variable anywhere else:

var work_computer_price = work_computer.price();



It's not going to work to do what you suggest because get_price is using this, which is not going to be set to the object he wants if he calls it like you're suggesting. This is the alternative:

this.price = get_price.call(this);



I've tried
this.price = get_price.call(this);


but it the laptop part is missing everything except for the price. I kept looking through the chapter and I can't find how to use call in there.
Was This Post Helpful? 0
  • +
  • -

#8 brian28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-March 14

Re: basic basic javascript question

Posted 09 May 2014 - 02:12 AM

View PostBlindman67, on 08 May 2014 - 09:59 AM, said:

Oops my bad. Sorry OP I should have looked deeper.

no prob. I fixed it
Was This Post Helpful? 0
  • +
  • -

#9 brian28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-March 14

Re: basic basic javascript question

Posted 09 May 2014 - 02:21 AM

[quote name='brian28' date='08 May 2014 - 08:52 PM' timestamp='1399607576' post='2008721']
thank you i appreciate the help

View PostArtificialSoldier, on 08 May 2014 - 09:43 AM, said:

Quote

At line 1 you have a function that is named get_price so on line 15 you are assigning this.price the contents of get_price which is the source code of the function. That is obviously not what you want.

No, that's correct. He is assigning a function as a method of the object. This is the problematic line where he is referring to the method but not calling it:

document.write("<strong>Price:</strong> $"+work_computer.price);



He's doing it correctly here, but then he doesn't use that variable anywhere else:

var work_computer_price = work_computer.price();



It's not going to work to do what you suggest because get_price is using this, which is not going to be set to the object he wants if he calls it like you're suggesting. This is the alternative:

this.price = get_price.call(this);



I've fixed it thank you for the help. Couldn't find the edit button to delete the last message.
Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2063
  • View blog
  • Posts: 6,327
  • Joined: 15-January 14

Re: basic basic javascript question

Posted 09 May 2014 - 11:10 AM

For your reference, the Function.call method is described here:

https://developer.mo...s/Function/call

Basically, it's a way to call a function using a particular scope that you specify (the scope is what the function refers to when you use this). Since functions in Javascript are objects, all functions have methods like call, apply, and bind that you can use.
Was This Post Helpful? 0
  • +
  • -

#11 brian28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-March 14

Re: basic basic javascript question

Posted 09 May 2014 - 11:29 PM

View PostArtificialSoldier, on 09 May 2014 - 11:10 AM, said:

For your reference, the Function.call method is described here:

https://developer.mo...s/Function/call

Basically, it's a way to call a function using a particular scope that you specify (the scope is what the function refers to when you use this). Since functions in Javascript are objects, all functions have methods like call, apply, and bind that you can use.


ook, thank you for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1