Home>

Previous words

The parameters of javascript functions are different from those of most other languages.The function doesn't mind how many arguments are passed in,I do n’t care what data type the parameters passed in,You can even pass no parameters.

arguments

The function definition in JavaScript does not specify the type of the function parameter,The function call also does not perform any type checking on the argument values ​​passed in.In fact, javascript function calls don't even check the number of incoming parameters

function add (x) {
return x + 1;
}
console.log (add (1));//2
console.log (add ("1"));//"11"
console.log (add ());//nan
console.log (add (1,2));//2

Formal parameter

non- In non-strict mode,A parameter of the same name can appear in a function,And only the last parameter of that name appears

function add (x, x, x) {
return x;
}
console.log (add (1,2,3));//3

In strict mode,A formal parameter with the same name will throw a 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 context

Number of parameters

When the actual parameter is less than the number of formal parameters specified in the function declaration,The remaining parameters will be set to undefined

function add (x, y) {
console.log (x, y);//1 undefined
}
add (1);

Often use logical OR operator 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] Actually, using y || 2 is not strict,Explicitly setting false values ​​(undefined, null, false, 0, -0, "", nan) will get the same result.So it should be set appropriately according to the actual scene

When the number of actual reference parameters is large,The remaining arguments cannot be obtained directly,Need to use the arguments object that will be mentioned

The parameters in JavaScript are internally represented by an array.The function always receives this array.It doesn't matter which parameters are in the array.This parameter array can be accessed through the arguments object in the function body,Thereby getting every parameter passed to the function.The arguments object is not an instance of array, it is an array-like object,Each element of it can be accessed using square bracket syntax

function add (x) {
console.log (arguments [0], arguments [1], arguments [2]) //1 2 3
return x + 1;
}
add (1,2,3);

Lengtharguments object's length property shows the number of actual parameters,Function's length property shows the number of formal parameters

function add (x, y) {
console.log (arguments.length) //3
return x + 1;
}
add (1,2,3);
console.log (add.length);//2

Sturgeon is just a convenience,But not required

function add () {
return arguments [0] + arguments [1];
}
console.log (add (1,2));//3

Object parameter

When a function contains more than 3 formal parameters,Keep in mind that the correct order of the arguments in the calling function can be a headache

function arraycopy (/* array */from,/* index */form_start,/* array */to,/* index */to_start,/* integer */length) {
//todo
}

Incoming Pass parameters in the form of name/value pairs,So the order of the parameters doesn't matter.When defining a function,The passed arguments are written into a separate object,Pass in an object when calling,Name/value pairs in the object are the actual 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});

Synchronize

When the number of shape participation parameters is the same,The values ​​of the arguments object and the corresponding formal parameters are kept in sync

function test (num1, num2) {
console.log (num1, arguments [0]);//1 1
arguments [0]=2;
console.log (num1, arguments [0]);//2 2
num1=10;
console.log (num1, arguments [0]);//10 10
}
test (1);

note [Note] Although the named parameter and the corresponding arguments object have the same value,But not the same namespace.Their namespaces are independent,But the values ​​are synchronous

But 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 1
arguments [0]=2;
console.log (num1, arguments [0]);//1 2
num1=10;
console.log (num1, arguments [0]);//10 2
}
test (1);

shape When the formal parameter has no corresponding actual parameter,The values ​​of the arguments object do not correspond to the values ​​of the formal parameters

function test (num1, num2) {
console.log (num1, arguments [0]);//undefined, undefined
num1=10;
arguments [0]=5;
console.log (num1, arguments [0]);//10,5
}
test ();

Internal attributes

[Callee]

Arguments object has a property called callee, which is a pointer,Pointer to the function that owns the arguments object

Here is the classic factorial function

function factorial (num) {
if (num<= 1) {
return 1;
} else {
return num * factorial (num-1);
}
}
console.log (factorial (5));//120

However, the execution of the above function is tightly coupled with the function name.You can use arguments.callee to eliminate function decoupling

function factorial (num) {
if (num<= 1) {
return 1;
} else {
return num * arguments.callee (num-1);
}
}
console.log (factorial (5));//120

But in strict mode,Accessing this property will throw a typeerror

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 them
console.log (factorial (5));

At this time, you can use named function expressions

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 a function holds a reference to the function that called 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

function inner () {
"use strict";
//typeerror:"caller" and "arguments" are restricted function properties and cannot be accessed in this context
console.log (inner.caller);
}
inner ();

[2] caller of arguments object

Attributes The property is always undefined. This property is defined to distinguish between arguments.caller and the caller property of the function

function inner (x) {
console.log (arguments.caller);//undefined
}
inner (1);

Similarly, in strict mode,Accessing this property will throw a typeerror

function inner (x) {
"use strict";
//typeerror:"caller" and "arguments" are restricted function properties and cannot be accessed in this context
console.log (arguments.caller);
}
inner (1);

Function overloading

Javascript functions cannot be overloaded in the traditional sense.In other languages,You can write two definitions for a function,As long as the two defined signatures (type and number of parameters accepted) are different

Javascript function is not signed,Because its parameters are represented by an array containing 0 or more values.Without a function signature,Real overloading is impossible

//The later statement overwrites the previous statement
function addsomenumber (num) {
return num + 100;
}
function addsomenumber (num) {
return num + 200;
}
var result=addsomenumber (100);//300

Only by checking the type and number of parameters in the passed function and reacting differently,To mimic method overloading

function doadd () {
if (arguments.length == 1) {
alert (arguments [0] + 10);
} else if (arguments.length == 2) {
alert (arguments [0] + arguments [1]);
}
}
doadd (10);//20
doadd (30,20);//50

Parameter passing

The parameters of all functions in JavaScript are passed by value.That is,Copy the value outside the function to the parameter inside the function,Just like copying a value from one variable to another

[1] Basic type value

transfer When passing a value of a basic type to a parameter,The passed value is copied to a local variable (named parameter or an element of the 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

transfer When passing a value of reference type to a parameter,Will copy the address of this value in memory 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 rewriting a parameter of a reference type inside a function,This variable refers to a local object.And this local object will be destroyed immediately after the execution of the function

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);
  • Previous Detailed installation and configuration of SVNServer under Windows
  • Next Reverse Ajax in 30 minutes