Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Tim Bray on 'What Sun Should Do'

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

More frequently sought solutions

Our esteemed columnist tackles 10 more of your most common JavaScript questions

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Last November, the JavaScript column in this illustrious publication presented a series of "frequently sought solutions" -- 10 of the most commonly asked-for JavaScript code solutions. The column struck a nerve and you asked for more. I can't turn away such a worthy bunch, so this month, we'll tackle 10 more frequently asked questions. Specifically, we'll find out how to:



  • Pass a nonstandard number of parameters to one function
  • Play a script when starting Netscape
  • Link to one page for JavaScript browsers and another page for other browsers
  • Process a link before loading a new document
  • Display a link for JavaScript users only
  • Use string methods with the window.location object
  • Display a clock on a page
  • Display the document source/document info windows
  • Link to a different page if the user's browser does not support JavaScript
  • Wait for a period of time before executing JavaScript code


That's a lot to cover, so let's get going. SUBHEAD: Pass a nonstandard number of parameters to one function Functions are typically "formally" declared to accept a certain number of parameters, and standard programming practice dictates that you always call a function with the right number of parameters. For example, this script uses a showMessage function that is formally declared to accept two parameters -- Message1 and Message2 -- which are used as messages in an alert box:



<![if !supportEmptyParas]> <![endif]>
showMessage ("Hello", "There");
<![if !supportEmptyParas]> <![endif]>
function showMessage (Message1, Message2) {
      alert (Message1);
      alert (Message2);
}


But JavaScript is more flexible than this. You can define a function with no formal list of parameters and call it using any number of parameters -- or no parameters, if you wish. JavaScript supports a special arguments object -- an array -- that is created inside a function. In addition to a length array that specifies the number of arguments passed to the function, each element of the array is a separate parameter passed to the function from the original "caller." For example, if you pass the revised showMessage function two parameters, the arguments object contains these values:



<![if !supportEmptyParas]> <![endif]>
arguments.length = 2
arguments[0] = "Hello"
arguments[1] = "There"


Code inside your function can determine how many arguments to process, and then go through the arguments array, one element at a time, and pick out the arguments that were passed to the function. Here's the revised version of the showMessage function, which is able to accept any number of parameters:



<![if !supportEmptyParas]> <![endif]>
showMessage ("Hello", "There");
showMessage ("Hello", "There", "Again!");
<![if !supportEmptyParas]> <![endif]>
function showMessage () {
      for (var Count = 0; Count < showMessage.arguments.length; Count++) {
              alert (showMessage.arguments[Count]);
      }
}


JavaScript also supports a caller property of the arguments object. This property contains the object string of the function that called this function. This means that you can use the caller property to determine the name of the "calling" function, and then use that name to determine what course of action to take. You might have your function do one thing if X function calls it, and do something else if Y function calls it. Here's a short demonstration of the caller property, along with some code to extract just the name of the calling function. This name is displayed in an alert box:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources