Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Syntax for passing values

    What is the syntax for passing values from one function to another?

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

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post
    Syntax is always same :

    Code:
    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";
    	else
    		return sNamePassed;
    }
    
    function setAge(iAgePassed)
    {
    	if (isNaN(iAgePassed)
    		return 0;
    	else
    		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.
    Last edited by Zvona; 07-30-2002 at 08:26 AM.
    Zvona
    First Aid for
    Web Design

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 )


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •