Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

JavaScript Workshop Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Spacing between combined variables

 
Post new topic   Reply to topic    JSWorkshop Forum Index -> JavaScript
View previous topic :: View next topic  
Author Message
SilverM
New member
New member


Joined: 25 Jan 2011
Posts: 6

PostPosted: Tue Jan 25, 2011 6:53 pm    Post subject: Spacing between combined variables Reply with quote

Hello, I'm a student with a question about 'cleaner' JavaScript. I'm writing a basic function that takes someone's first and last names, then combines them in a alert box. Here's a snippet of the function in question:

Code:

<script type="text/javascript">
   function Greet(who1,who2) {
      alert("Greetings, " + who1 + " " + who2);
   }
</script>


My question is, does JavaScript include anything that acts as a spacer automatically when dealing with variables? As you can see, right now I've jury-rigged a space in between the two variables, but I would like to know if there's a better way. Thanks in advance!

Full JS code below:
Code:

<html>
<head>

<script type="text/javascript">
   function Greet(who1,who2) {
      alert("Greetings, " + who1 + " " + who2);
   }
</script>
</head>

<body>
<script language="JavaScript">
   who1=prompt("What is your first name?");
   who2=prompt("What is your last name?");
   Greet(who1,who2);
</script>
</body>
</html>
Back to top
View user's profile Send private message
Jen
New member
New member


Joined: 27 Jan 2011
Posts: 8

PostPosted: Thu Jan 27, 2011 9:07 pm    Post subject: Shorter code Reply with quote

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type"content="text/html;charset=utf-8" />
<title></title>
<script type="text/javascript">
function Greet (who1, who2) {
alert("Greetings, " + who1, who2);
}
var who1=prompt ("What is your name?");
var who2=prompt ("What is your name?");
</script>

</head>
<body>
<script type="text/javascript">
Greet (who1 + " and " +who2);
</script>
</body>
</html>


In the alert function itself, I just called the parameters. I arranged the spacing in the call down below. Hope this helps:)
Back to top
View user's profile Send private message
sohnee
Senior Member
Senior Member


Joined: 17 Jul 2002
Posts: 2077
Location: UK

PostPosted: Fri Jan 28, 2011 3:32 am    Post subject: Reply with quote

Hello SilverM,

Your example is actually exactly how you should do it in JavaScript.

There isn't a string.Format (.NET) or in-string variable (PHP) way of doing this, so combining the strings as you have done is spot on.

Jen has given a different take on the subject, but I think you should stick with what you've got - here's why.

The CALLER (i.e. when you call the function) shouldn't be concerned with how the name is displayed. It passes the first name and surname and defers all knowledge of execution to the function. It is the purpose of the function to know how to display the name.

Also, in Jen's example, she passes the concatenated name as parameter who1, and no second parameter, so who2 will be empty in the function.

Lastly, I think you should rename a few things just to make everything crystal clear... Here is my full example.


Code:
<html>
<head>

<script type="text/javascript">
   function Greet(firstName, lastName) {
      alert("Greetings, " + firstName + " " + lastName);
   }
</script>
</head>

<body>
<script type="text/javascript">
   var firstNameInput = prompt("What is your first name?");
   var lastNameInput = prompt("What is your last name?");
   Greet(firstNameInput, lastNameInput);
</script>
</body>
</html>


So in the function, I have named the parameters firstName and lastName - that tells you exactly what is expected for the function.

In the calling script block, I have declared the prompt results with "var". I have also named them firstNameInput and lastNameInput - it is always clearer to avoid using the same name for variables in a different scope, but is also tells you a bit more about the variable.

The function doesn't care where the firstName and lastName come from, but you can tell by looking at firstNameInput that it has been supplied by the user (without specifically specifying it was via a prompt - it could be from a text box, for example).

Lastly, I have replaced language="JavaScript" with the type="text/javascript" - which is the correct way to declare a script block. You had used the correct way in your first block and the obsolete way in the second.

I hope this helps.
_________________
I also work on... Steve Fenton's Blog and contribute to The Enhance PHP Unit Testing Framework
Back to top
View user's profile Send private message Visit poster's website
SilverM
New member
New member


Joined: 25 Jan 2011
Posts: 6

PostPosted: Sun Jan 30, 2011 5:25 pm    Post subject: Reply with quote

Thanks for the clear responses Jen and especially Sohnee! This puts my mind at ease. Thanks also for the heads-up about the obsolete way to define a JavaScript block.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    JSWorkshop Forum Index -> JavaScript All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.