javascript_logo_unofficial-300x300

Currently, Javascript language is not strangers for programmers . Today , I would like to introduce management parameters passed to a function in the javascript language . Unlike languages ​​such as C , C + + , Java … then the variable declaration should be cleared and the parameter passed to a function is fixed , if you want to pass the parameters more or less to the function defined before that it is impossible ( just as you can use methology like overloading in function) . So what is the difference ?

Javascript manage the parameters passed to the function through an array named arguments this is  a global array.

For example :

I declare a function as follows

function reciveArgument( ) {

/ / this function will return parameters that you passed

return arguments ;

}

and when I call this function with different parameters as following :

console.log ( reciveArgument ( ” Parameter 1 ” , ” Parameter 2 ” , ” Parameter 3 ” ) ) ;

when you open up your console in your browser and see

>>>[“Parameter 1”, “Parameter 2”, “Parameter 3”]

Try passing by numbers

console.log ( reciveArgument ( 1,2,3 ) ) ;

The  results returned as following:

>>[1, 2, 3]

So the arguments array is available in the function we do not need to care about  overload function . For example, you write function sum ( ) of natural numbers , then just do a shoulder operation as follows :

function sum( ) {

var total = 0 ;

for ( var i in arguments) {
total + = arguments [ i ] ;
}
return total;

}

and you call this function again with different parameters

console.log ( sum ( 1,2,3 ) ) ;

console.log ( sum ( 1,2,3,4,5 ) ) ;

The  results returned as following:

6

15

When you research the JavaScript language , you will find  more interesting things . Thank you

Contact Us

お問合せはこちらから

ラボ型開発・オフショア開発・HTML5コーディング・スマホアプリ・グミに対する質問などお気軽にご連絡ください。

Contact Us