The first record of the new year, starting from here, first a simple! Last year's knowledge comb left a lot of tails for a lot of reasons (lack of knowledge, lazy, projects more ...) LG: All the Excuses ~ Well, I admit, this is all an excuse, this year must clean up the tail!
Here is the first case of knowledge grooming:
- Write a summation method SUMFN, regardless of the parameters passed, can be final and calculated, and returned to the function outside the use. (Requirements: A parameter does not pass the default result is 0, for the passing of the parameters of the abnormal number is not with the cumulative operation);
This case is mainly to examine the knowledge point is the arguments parameter, to give a close-up, what meaning?
Arguments parameter is a special object, is a built-in property of the current function, it is very much like an array, but it is not an array LG: Said what, or do not understand the amount, well, the example is more than all the eloquence, Let's give some Li Zilai a look:
?
1234567891011121314 |
function abc(a,b,c){
//看看arguments是什么
console.log(arguments);
}
abc(1,2,3) ;
//[1,2,3] 这不是数组吗
function abc(a,b,c){
//看看arguments是什么类型的
console.log(
typeof arguments)
}
abc(1,2,3) ;
//object 这是对象,不是数组
|
Look, is not a bit of meaning, like array of non-array LG: Then how does it use it ~ This question is good, look at the following code:
?
123456789 |
function Code class= "JavaScript plain" >abc (a,b,c) {      Code class= "javascript keyword" >var len = arguments.length;      console.log (len);      console.log (Arguments[0]);      console.log (arguments[1]); abc (All-in-a-box); //3, 1, 2 //length 3, first value is 1, second value is 2 ... appears to be a |
with a corresponding relationship
LG: That's the only 2? Not fun at all ~ Ah, you are anxious, of course, not only these two ah, it has a very practical function, that is, in the function, no need to explicitly indicate the parameter name, arguments can directly access it, you say dick not dick, ear to hear for the virtual, the eyes for:
?
12345678 |
function Code class= "JavaScript Plain" >abc () {      var a = arguments;      var num = Arguments[0] + arguments[1] + arguments[2]; //here can be used in loops, for the convenience of viewing, opened the writing, you understand the      console.log (num); abc (All-in-a-box); //6 description can get //Normally, the function has no parameters, Should be an error, if you can run successfully, indicating that it has this divine level |
LG: Sure enough, does it have any other skills for hanging and frying the sky? of course it is! There is a very useful property in the arguments object: callee. Arguments.callee returns the current function reference where this arguments object resides. It is recommended to use Arguments.callee instead of the function name itself when using recursive function calls. Give me a chestnut:
?
123456789 |
function abc(a){
if
(a==1){
console.log(arguments.callee);
//直接输出当前函数
return 1;
}
return a + arguments.callee(--a);
}
var mm = abc(10);
console.log(mm);
// 55<br><br>//arguments.callee(--a) 执行了递归调用,这样就完成了1~9的累加
|
See here, the above example should not be difficult to write, to follow my left hand a slow motion:
functionSumfn () {vararg =arguments; varsum = 0; if(arg = = "| | | arg = =" | | arg = =NULL) {sum= 0; }Else{ for(vari=0;i<arg.length;i++) {if(IsNaN (arg[i]) | | arg[i] = = "| | arg[i] = =") {sum=sum; }Else{sum+=Arg[i]; }}}returnsum;} varNUM1 = SUMFN (2) + SUMFN (3); Console.log (NUM1); //5 varnum2 = SUMFN (2,3,4); Console.log (num2); //9 varNUM3 =SUMFN (); Console.log (NUM3); //0
varNUM4 = SUMFN (1, ' E ', 6); Console.log (NUM4); //7
here, I don't know if you understand this example, LG: What about such a good thing, compatibility? haha, so hanging fried days of things, compatibility is also particularly good, you say Qi not irritating, tested, support all browsers!
Well, it is the object, it also has objects, but I have no object, Woo-woo ~ ~ ~, I'm going to find the object!
Hurry up! There is an incorrect or missing thing to understand, and hope criticizes! Appreciate it!
Source: http://www.cnblogs.com/liugang-vip/p/5201146.html
An instance of the arguments object is used