5,530,111 members and growing! (16,989 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Passing function arguments as parameters in JavaScript

By Tor2k

If you're in need of C-like input/output parameters or VB-like "by reference" function arguments in JavaScript, here's some help.
Javascript, HTMLWindows, NT4, Win2K, WinXP, Win2003, Visual Studio, IE 6.0, IE, Dev

Posted: 6 Aug 2005
Updated: 6 Aug 2005
Views: 62,470
Bookmarked: 13 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 2.26 Rating: 2.50 out of 5
2 votes, 25.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 37.5%
4
3 votes, 37.5%
5

Introduction

When used as function arguments in JavaScript, number and string objects are compared by value, and functions are compared by reference... array objects (arrays) seem to be the only option for passing arguments by reference, so we'll simply wrap an array of size 1 into a new object class that sets/gets the first element:

function param()
{
    this.array = new Array(1);

    this.setValue = function(v) { this.array[0] = v; }
    this.getValue = function()  { return this.array[0]; }
}

It's a simple object with a 1-element internal array that will hold anything (even an object reference), exposing two simple assignment and retrieval methods.

You would typically use it in situations where you need more than one return value.

// declare a variable as a parameter

var s = new param;

// issue a function call using the parameter

var o = output(dText.innerHTML, s, 'Hello, world.');

// the function is obviously designed to treat the 2nd argument as a parameter...

function output(arg1, arg2, arg3)
{
  ...
  // ...so that eventually it can assign a value to it.

  arg2.setValue(a + b % 10);
  ...
}

// when ready to retrieve the output value, use the retrieval function.

dResult.innerHTML += s.getValue() + ' (' + o + ')';

And that's it. You can declare and pass-in as many parameters as you like, as long as your code handles them through the member functions; you can also use the internal array property if you need to handle the argument as an array, say to concatenate with other arrays, etc.

Why not use a one-sized array directly? Good, valid question. Actually, I just wanted to improve the code's readability, and this looked better than handling the 0th element around in functions. Maybe it's a waste of time, but hey, I'm into the "slow down" way of things now.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tor2k


Born April 27, 1968 in Caracas, Venezuela, Hector J. Rivas has 24+ years of experience managing hardware and software development under many different operating systems, platforms and languages. He has developed microcontroller interfaces and PC games; authored computer based training lessons and delivered fully functional financial and administrative data-intensive applications, as well as image processing and other calculation-intensive applications. Mr. Rivas has also managed Y2K remediation, large scale platform migration and Web site projects, from R&D to actual deployment.
Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAwfulsussAnonymous7:27 10 Aug '05  
GeneralRe: AwfulmemberTor2k7:37 10 Aug '05  
GeneralRe: AwfulmemberSebastian Streiger9:51 14 Nov '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Aug 2005
Editor: Smitha Vijayan
Copyright 2005 by Tor2k
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project