Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript function parameters

JavaScript function parameter value without any examination.


Explicit function parameters (Parameters) and implicit parameters (Arguments)

In the previous tutorial, we have learned an explicit function parameters:

functionName (parameter1, parameter2, parameter3) {
// Code to be executed ......
}

Function parameters explicitly listed in the function definition.

Implicit function arguments passed to a function when the real value of the function call.


Parameter rule

JavaScript function definition display parameter is not specified data type.

JavaScript function implicit type parameter is not detected.

JavaScript function of the number of implicit parameter is not detected.


The default parameters

If the function is not available when you call an implicit parameter, the parameter is set to default: undefined

Sometimes this is acceptable, but it would be best to set a default value for a parameter:

Examples

function myFunction (x, y) { if (Y === undefined) { y = 0;} }

try it"

Or, easier way:

Examples

function myFunction ( x , y ) { y = y || 0 ; }

try it"

Note If y has been defined, y || return y because y is true, otherwise it returns 0, because the undefined is false.

Too many parameters If the function is set, the parameters will not be quoted, because they can not find the corresponding parameter name. Use only the arguments object to call.


Arguments Object

JavaScript function has a built-in objects arguments object.

argument object contains a parameter array function call.

You can easily find the value of the last parameter in this way:

Examples

x = findMax (1, 123, 500, 115, 44, 88); function findMax () { var i, max = 0; for (I = 0;. I < arguments length; i ++) { if (Arguments [i]> max) { max = arguments [i];} } return max;}

try it"

Or create a function to count all values ​​and:

Examples

x = sumAll (1, 123, 500, 115, 44, 88);

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

try it"


Passing parameters by value

Arguments in the function call is an implicit function of the parameters.

JavaScript implicit parameter passed by value: function just to get the value.

If the function to modify the value of a parameter, it does not modify the initial value of the explicit parameter (defined outside a function).

Change implicit argument outside the function is not visible.


Pass parameters through the object

In JavaScript, the value can be referenced object.

Therefore, we modify an object's properties within the function will modify its initial value.

Modify object properties outside the function can be applied to (global variables).

Modify object properties outside the function is visible.