|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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:
window.location object
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:
·
·
·
·