Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2002, 01:34 AM   PM User | #1
Graeme Hackston
Regular Coder

 
Join Date: Jun 2002
Posts: 624
Thanks: 0
Thanked 0 Times in 0 Posts
Graeme Hackston is an unknown quantity at this point
Combining Functions with Parameters?

I have 3 functions in which 75% of their statements are the same. Can I combine them into 1 function using parameters? If so, how do I call a parameter from a link and define them in the function?


I believe the function and links that call it would look something like this:

function One(WhatDoIPutHere) {

all common statements here;

if (Link1) {
do stuff;
}

if (Link2) {
do stuff;
}

if (Link3) {
do stuff;
}
}

<a href="javascript:void(0);" onmousedown="One('Link1')">Link1</a>

<a href="javascript:void(0);" onmousedown="One('Link2')">Link2</a>

<a href="javascript:void(0);" onmousedown="One('Link3')">Link3</a>


Thanks for your help.
Graeme Hackston is offline   Reply With Quote
Old 06-19-2002, 01:48 AM   PM User | #2
JohnKrutsch
Regular Coder

 
Join Date: Jun 2002
Location: The Planet Earth Code Poet: True
Posts: 282
Thanks: 0
Thanked 1 Time in 1 Post
JohnKrutsch is an unknown quantity at this point
Here is one way you can do it:

function One(num) {

all common statements here;

if (num=='Link1') {
do stuff;
}

if (num=='Link2') {
do stuff;
}

if (num=='Link3') {
do stuff;
}
}
JohnKrutsch is offline   Reply With Quote
Old 06-19-2002, 01:52 AM   PM User | #3
Graeme Hackston
Regular Coder

 
Join Date: Jun 2002
Posts: 624
Thanks: 0
Thanked 0 Times in 0 Posts
Graeme Hackston is an unknown quantity at this point
Thanks John.

I understand what == does but what is "num"? Is it an abreviation for number?
Graeme Hackston is offline   Reply With Quote
Old 06-19-2002, 04:31 AM   PM User | #4
JohnKrutsch
Regular Coder

 
Join Date: Jun 2002
Location: The Planet Earth Code Poet: True
Posts: 282
Thanks: 0
Thanked 1 Time in 1 Post
JohnKrutsch is an unknown quantity at this point
num was just an arbitrary variable name I choose. In my mind it did mean number but it really could be anything you want. Choose a vairbale name that makes sense to you. The variable name will represent the parameter that you passed in. It could just as easily been:


function One(fredtheVariableIamIam) {

all common statements here;

if (fredtheVariableIamIam=='Link1') {
do stuff;
}

if (fredtheVariableIamIam=='Link2') {
do stuff;
}

if (fredtheVariableIamIam=='Link3') {
do stuff;
}
}

Last edited by JohnKrutsch; 06-19-2002 at 04:42 AM..
JohnKrutsch is offline   Reply With Quote
Old 06-19-2002, 04:36 AM   PM User | #5
Graeme Hackston
Regular Coder

 
Join Date: Jun 2002
Posts: 624
Thanks: 0
Thanked 0 Times in 0 Posts
Graeme Hackston is an unknown quantity at this point
LOL thanks John.
Graeme Hackston is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:45 PM.


Advertisement
Log in to turn off these ads.