- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
directory
Function is introduced
The number of parameters
与The number of arguments
caller
forInvokes a reference to the current function
callee
forThe function itself
callee
Case study:All recursions:
(Factorial function, Fibonacci sequence…)
Introduction to zero functions
1) Function definition
Designed to encapsulate code, it is a block of code that can be executed repeatedly at any time
2) Pay attention to the function
- A function may take corporeal or no parameters
- A function may or may not have a return value
The role of the return
Similar to break, no statement can be written after a return. Break is used to terminate a switch statement or loop immediately.Return Ends the current function immediately
2) arguments
4 ways to convert a class array to an array
function fn(a,b){
let args = [...arguments];
let args1 = Array.from(arguments);
let args2 = [].slice.call(arguments);
let args3 = Array.prototype.slice.call(arguments);
console.log(arguments, args, args1, args2, args3);
};
fn(1.2);
Copy the code
3) The realization of the slice [] = > []
或 Class array => []
The call method points the splice object to the argument
Array.prototype.slice = function(start,end){
var result = new Array(a); start = start ||0;
end = end || this.length;
// This refers to the called object. When call is used, the key is to be able to change the reference of this to the object passed in
for(var i = start; i < end; i++){
result.push(this[i]);
}
return result;
};
let a =[1.2.3.4]; let b = a.slice1();
console.log(b); / / [1, 2, 3, 4]
Copy the code
oneThe number of parameters
与 The number of arguments
The number of parameters
:The function name. Length
orarguments.callee.length
.The number of arguments
:arguments.length
function fn(a,b,c){
console.log('Number of parameters :',fn.length);
console.log('Number of parameters :'.arguments.callee.length);
console.log('the arguments object:.arguments);
console.log('Number of arguments :'.arguments.length);
// Callee returns a reference to the function being executed (this is often used to recurse the anonymous function itself but is not feasible in strict mode)
// Callee is a member of the Arguments object which represents a reference to the function object itself and has a length attribute (which represents the length of the parameter)
console.log('Function itself :'.arguments.callee === fn);
console.log('Function caller :',fn.caller); // Caller returns a reference to the current function or null if it is called from the top level
console.log('Function name :',fn.name);
}
fn(1.2.3);
// The number of parameters is 3
// The number of parameters is 3
ƒ [1, 2, 3, callee: ƒ, Symbol(symbol.iterator): ƒ]
// The number of arguments is 3
// The function itself: false
// Function caller: null
// The function name is fn1
Copy the code
twocaller
forInvokes a reference to the current function
Caller = caller; callee = called
ECMAScript 5 also normalizes the caller property of another function object. This property holds a reference to the function calling the current function, null if the current function is called in global scope.
var callerTest = function() {
console.log(callerTest.caller); };function a() {
callerTest() ;
}
callerTest() ;/ / output is null
a(); // a() { callerTest(); }
Copy the code
Caller can assume that the function calls it, and it points to whomever
threecallee
forThe function itself
Callee is the function itself, called recursively using the scenario
Arguments is a class array object that holds all the arguments passed into the function. This object has a property called Callee, which is a pointer to the function that owns the Arguments object
1) the case:All recursions:
(Factorial function, Fibonacci sequence…)
Recursive calls are problematic when the function name is changed.
function factorial(num){
if(num === 1) {return 1;
}else{
return num * factorial(num - 1); }}console.log(factorial(5)); / / 120
// Call recursively: there is a problem when the function name is changed
var fn1 = factorial;
console.log(fn1(5)); / / 120
factorial = function(){
return 0
};
// The result of the function execution is incorrect
console.log(fn1(5));/ / 0
console.log(factorial(5));/ / 0
Copy the code
So, using arugments.callee can solve the problem of function names being modified.
function factorial(num){ if(num === 1){ return 1; }else{ return num * arguments.callee(num - 1); }}Copy the code
There is no function name in the body of the rewritten function, so the recursion can be guaranteed when the referenced function uses any name.
reference
- Implementation of currization of functions
conclusion
The number of parameters
:The function name. Length
orarguments.callee.length
.The number of arguments
:arguments.length
Caller B. caller C. caller D. caller
.Callee translates as the called
callee
forThe function itself
, Application scenariosRecursive calls
caller
You can thinkThat function called it
.It points to whoever it is
- Note:
callee
andcaller
There are problems in strict mode. Arguments to hold the arguments to the function
.Callee points to the current function
.Caller refers to a function that calls the current function
An array of class
Turn an array
theFour kinds of way