The previous words
The parameters of javascript functions are different from those of functions in most other languages. Functions don't mind how many parameters are passed in, nor do they care about the data type of the parameters passed in, and they can even not pass parameters.
arguments
The function definition in javascript does not specify the type of function parameters, and the function call does not perform any type check on the passed parameter values. In fact, javascript function calls do not even check the number of incoming formal parameters.
function add(x){return x+1;}console.log(add(1));//2console.log(add('1'));//'11'console.log(add());//NaNconsole.log(add(1,2));//2Formal parameters of the same name
In non-strict mode, a formal parameter of the same name can appear in the function, and only the last formal parameter of the name that appears can be accessed
function add(x,x,x){return x;}console.log(add(1,2,3));//3In strict mode, a syntax error occurs when the same name is involved in the syntax error.
function add(x,x,x){'use strict';return x;}console.log(add(1,2,3));//SyntaxError: Duplicate parameter name not allowed in this contextNumber of parameters
When the specified number of formal parameters declared by the real reference function is small, the remaining formal parameters will be set to undefined values
function add(x,y){console.log(x,y);//1 undefined}add(1);Use logic or operators often to set a reasonable default value for omitted parameters
function add(x,y){y = y || 2;console.log(x,y);//1 2}add(1);[Note] In fact, using y || 2 is not rigorous, explicitly setting false values (undefined, null, false, 0, -0, '', NaN) will also get the same result. Therefore, it should be set reasonably based on the actual scenario
When there are many real arguments, the remaining real arguments cannot be obtained directly. You need to use the arguments object mentioned soon.
Parameters in javascript are internally represented by an array. The function always receives this array, and does not care about which parameters are contained in the array. This parameter array can be accessed through the arguments object in the function body, thereby obtaining each parameter passed to the function. The arguments object is not an instance of Array, it is an array object that can access every element of it using square bracket syntax.
function add(x){console.log(arguments[0],arguments[1],arguments[2])//1 2 3return x+1;}add(1,2,3);The length attribute of the arguments object shows the number of real parameters, and the length attribute of the function shows the number of formal parameters
function add(x,y){console.log(arguments.length)//3return x+1;}add(1,2,3);console.log(add.length);//2Formal parameters are only convenient, but not necessary
function add(){return arguments[0] + arguments[1];}console.log(add(1,2));//3Object parameters
When a function contains more than 3 formal parameters, it is really a headache to remember that the correct order of the actual parameters in the function is called.
function arraycopy(/*array*/from,/*index*/form_start,/*array*/to,/*index*/to_start,/*integer*/length){//todo}Pass the parameters in the form of name/value pairs, so the order of the parameters is irrelevant. When defining a function, the passed in real parameters are written into a separate object. An object is passed in when called. The name/value pairs in the object are the real parameter data that is really needed.
function easycopy(args){arraycopy(args.from,args.form_start || 0,args.to,args.to_start || 0, args.length);}var a = [1,2,3,4],b =[];easycopy({form:a,to:b,length:4});synchronous
When the number of actual parameters of the form participating in the same shape, the value of the arguments object and the value of the corresponding formal parameters remain synchronized
function test(num1,num2){console.log(num1,arguments[0]);//1 1arguments[0] = 2;console.log(num1,arguments[0]);//2 2num1 = 10;console.log(num1,arguments[0]);//10 10}test(1);[Note] Although the named parameters and the corresponding arguments object's value are the same, they are not the same namespace. Their namespaces are independent, but the values are synchronous
However, in strict mode, the values of the arguments object and the values of the formal parameters are independent
function test(num1,num2){'use strict';console.log(num1,arguments[0]);//1 1arguments[0] = 2;console.log(num1,arguments[0]);//1 2num1 = 10;console.log(num1,arguments[0]);//10 2}test(1);When the formal parameters do not have corresponding actual parameters, the value of the arguments object does not correspond to the value of the formal parameters
function test(num1,num2){console.log(num1,arguments[0]);//undefined,undefinednum1 = 10;arguments[0] = 5;console.log(num1,arguments[0]);//10,5}test();Internal properties
【callee】
The arguments object has a property called callee, which is a pointer to the function that owns the arguments object.
Below is the classic factorial function
function factorial(num){if(num <=1){return 1;}else{return num* factorial(num-1);}} console.log(factorial(5));//120However, the execution of the above function is closely coupled with the function name, and the function decoupling can be eliminated using arguments.callee
function factorial(num){if(num <=1){return 1;}else{return num* arguments.callee(num-1);}} console.log(factorial(5));//120But in strict mode, accessing this property will throw a TypeError error
function factorial(num){'use strict';if(num <=1){return 1;}else{return num* arguments.callee(num-1);}} //TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to themconsole.log(factorial(5));At this time, a named function expression can be used
var factorial = function fn(num){if(num <=1){return 1;}else{return num*fn(num-1);}}; console.log(factorial(5));//120【caller】
There are actually two caller attributes
【1】Function caller
The caller property of the function holds a reference to the function that calls the current function. If the current function is called in the global scope, its value is null
function outer(){inner();}function inner(){console.log(inner.caller);//outer(){inner();}}outer(); function inner(){console.log(inner.caller);//null}inner();In strict mode, accessing this property will throw a TypeError error
function inner(){'use strict';//TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this contextconsole.log(inner.caller);}inner();【2】Arguments object caller
This property is always undefined. This property is defined to distinguish the caller properties of arguments.caller and function
function inner(x){console.log(arguments.caller);//undefined}inner(1);Similarly, in strict mode, accessing this property will throw a TypeError error
function inner(x){'use strict';//TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this contextconsole.log(arguments.caller);}inner(1);Function overloading
JavaScript functions cannot implement overloading like in the traditional sense. In other languages, two definitions can be written for a function, as long as the signatures of these two definitions (types and quantities of accepted parameters) are different
The javascript function has no signature because its parameters are represented by an array containing 0 or more values. Without function signatures, real overloading is impossible
//The subsequent declaration overrides the previous declaration function addSomeNumber(num){return num + 100;}function addSomeNumber(num){return num + 200;}var result = addSomeNumber(100);//300Overloading of methods can only be simulated by checking the types and quantities of parameters in the passed function and making different reactions.
function doAdd(){if(arguments.length == 1){alert(arguments[0] + 10);}else if(arguments.length == 2){alert(arguments[0] + arguments[1]);}}doAdd(10);//20doAdd(30,20);//50Parameter pass
All functions in javascript are passed by value. That is to say, copying the value outside the function to the parameters inside the function is the same as copying the value from one variable to another
【1】Basic type value
When passing a value of the primitive type to a parameter, the passed value is copied to a local variable (an element of a named parameter or arguments object)
function addTen(num){num += 10;return num;}var count = 20;var result = addTen(count);console.log(count);//20, no change console.log(result);//30【2】Reference type value
When passing a reference type value to the parameter, the address of this value in memory will be copied to a local variable, so the change of this local variable will be reflected outside the function
function setName(obj){obj.name = 'test';}var person = new Object();setName(person);console.log(person.name);//'test'When the formal parameters of the reference type are overridden inside the function, this variable refers to a local object. This local object will be destroyed immediately after the function is executed
function setName(obj){obj.name = 'test';console.log(person.name);//'test'obj = new Object();obj.name = 'white';console.log(person.name);//'test'}var person = new Object();setName(person);The above is the in-depth understanding of JavaScript function parameters introduced by the editor to you (recommended). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!