View Full Version : Syntax for passing values

07-29-2002, 10:28 PM
What is the syntax for passing values from one function to another?

Does syntax change if passing to or from an external javascript?

07-30-2002, 09:24 AM
Syntax is always same :

function Person(sFirstName, iAge)
var objPerson = new Object();
objPerson.firstName = setFirstName(sFirstName);
objPerson.age = setAge(iAge);
return objPerson

function setFirstName(sNamePassed)
if (sNamePassed == "")
return "John";
return sNamePassed;

function setAge(iAgePassed)
if (isNaN(iAgePassed)
return 0;
return Math.round(iAgePassed);
var personPeter = new Person("Peter",25);
creates a new object personPeter, which has properties firstName("Peter") and age(25).

var personUnknown = new Person();
creates a new object, which has properties firstName("John") and age(0).

It doesn't matter, where functions are located, as long as you've imported them. You could encapsulate functions above, for instance functions starting with set could be included and imported in setFunctions.js etc.

07-30-2002, 12:18 PM
Originally posted by Zvona
It doesn't matter, where functions are located, as long as you've imported them.

I think I know what you meant, but I believe this is a little bit misleading: When you call a function, then it has to be defined previously to that point in your code.

So if you have a lot of code in a <script> element located in your <head>, and that code makes use of a function located in an external script called functions.js, and that file gets imported in a <script> element within you <body> element, you will receive errors.

Ok, for experienced javascripters this may sound far too obvious. I'll just mention that because in other programming languages, things work a little bit differently.

07-30-2002, 04:03 PM
Thanks a lot for the info and example Zvona they really helped.

Thanks for the tip Mordred as I am not very experienced with JavaScript yet. (likes its not obvious:) )

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum