How to give context path in Javascript

Examples: parameters in JavaScript

Parameters are an important part of JavaScript - but they can do more than just pass values.

Variable parameter lists

In JavaScript - as an 'untyped' language - the user does not normally have to worry about the parameters of a function, they are simply determined by their position in the parameter list. But with higher demands, the limits of this freedom very soon become noticeable. In this series, you will learn how to overcome them.
JavaScript is an "untyped language" - in contrast to "strictly typed languages" such as C or C ++, where each variable must be declared with a certain data type before it can be used for the first time and can only be used with this type from now on. This has advantages in larger programs with a large number of variables that humans can easily confuse. So it is very good if the compiler marks every mix-up as faulty.

In JavaScript, however, there is no compiler, it is an "interpreter language". The interpreter is built into many browsers in the same way, but to a very different extent. The full JavaScript is only available in Microsoft Internet Explorer (MS-IE), which today, however, has a market share of 86%. If you write JavaScript programs that are supposed to work on different browsers, then you should definitely test them with all of them before uploading them to the Internet.

A common problem is to write a function with a variable number of parameters. (It's actually absurd, isn't it? The meaning of a parameter results from its position - but this is variable?)

Well, JavaScript is so universal that this apparent contradiction has been resolved: The meaning of a parameter is still determined by its position, but there is no upper limit for it. A function is only declared with a certain number of parameters, but it can be called with any number.

For example, the declaration means:

conceptually actually


Internally, a parameter is not accessed via its name, but is simply used like a field in an array. This array is called in JavaScript and forms part of the function declaration - it must therefore be qualified with the name of the respective function. Then the i-th parameter can be accessed with.
For every array there is an attribute in JavaScript. Since the length of the parameter list for the function declaration is not yet known, you need this attribute, as you can see in the following example.

For this example, it was assumed that a function was desired that simply calculates the sum of any number of parameters. Your declaration could look like this:

function sum (param)
{
var i, sum = 0;
for (i = 0; i sum + = sum.arguments [i];
return sum;
}

Different usage of parameters

In JavaScript functions, the parameters are determined by their position in the parameter list - one would think. With the operator, however, you can treat a parameter very differently, depending on how it was passed when the function was called.
A common problem when calling JavaScript functions is that people tend to mentally hide a detail when it is not relevant at the moment. The JavaScript interpreter in the browser takes this very carefully: if a parameter is "empty", then at least, (empty string) or for it should be used.
For programming productivity, it is therefore better to first examine a parameter before actually processing it. Depending on the output, this parameter was transferred as,,,, or when called and should be treated accordingly.
Try calling the following example function with different parameters:

function test (param)
{
switch (typeof (param))
  { 
case 'number': alert ('param =' + param + '(type: number)');
break;
case 'string': alert ('param =' + param + '(type: string)');
break;
case 'boolean': alert ('param =' + param + '(type: boolean)');
break;
case 'object': alert ('param =' + param + '(type: object)');
break;
case 'function': alert ('param =' + param + '(type: function)');
break;
case 'undefined': alert ('param =' + param + '(type: undefined)');
break;
   }
}

To test you will first create an object in HTML and then use its identifier to call this function, for example:

<BR id="x">
<SCRIPT>Test(x)</SCRIPT>

A common case where you can make your life a lot easier with it is a function whose parameter list ends with two parameters, which are only used in certain cases, but otherwise simply left out:

function xyz (... ... ..., options, debug)
{
  ... 
}

If needed, parameter is a (which controls some of the subtleties of processing). As the name suggests, it is "optional" - that is, it is not needed in every case.
Parameter is one that controls the degree of fineness of the intermediate outputs during software development - but is never needed in productive use.
What does the programmer do during software development when he wants to debug such a function, but without it? - He simply calls them up with a numeric parameter at the end of the parameter list and doesn't care about them - for example:

function xyz (... ... ..., 1)

Because he knows that this function was declared as follows:

function xyz (... ... ..., options, debug)
{
if (typeof (options) == 'number')
  { 
debug = options; options = undefined;
  }
  ...
}

Parameters in the form of JavaScript code

"A JavaScript function clearly defines the algorithm for processing the parameters" - but only for relatively simple applications. You can also pass a parameter as a string which "coincidentally" corresponds to exactly one JavaScript code. This code is then evaluated internally and, if necessary, executed - thanks to the method.
When a JavaScript function is called, the user may want an action, depending on a condition. This condition will reasonably be formatted as it would be expressed in JavaScript.
Then this condition can be evaluated using the method, as you can see in the following example:

function evaluation (cond, x)
{
if (eval (cond))
alert ("x (=" + x + ") fulfills condition '" + cond);
else
alert ("x (=" + x + ") meets condition '" + cond + "not");
}

Call this function once! For example with the parameters on:

Evaluate ("x> 5", 6);
Evaluate ("x> 5", 3);

is an enormously versatile tool. You can use it not only to evaluate a string (if it "happens" to correspond to the JavaScript notation), but also to execute a JavaScript string. For example, two more parameters could be introduced into the function of the above example, which specify what should happen in one case and what in the other:

function evaluation (cond, x, operation1, operation2)
{
if (eval (cond))
eval (operation1);
else
eval (operation2);
}

What do you mean, what happens when you enable this feature with the following two calls? - Try it out!

Evaluate ("x> 5", 3, "alert ('x =' + x + '- condition fulfilled')",
"alert ('x =' + x + '- condition not met')");
Evaluate ("x> 5", 6, "alert ('x =' + x + '- condition fulfilled')",
"alert ('x =' + x + '- condition not met')");

This even works if one of the two operations is omitted, for example:

Evaluate ("x> 5", 6, "alert ('x =' + x + '- condition fulfilled')");
Evaluate ("x> 5", 3, "alert ('x =' + x + '- condition fulfilled')");
Evaluate ("x> 5", 7, "alert ('x =' + x + '- condition fulfilled')");

The second call is internal, so it is not executed. Only after the third call does a box appear on the screen.

about us