Home >Web Front-end >JS Tutorial >Detailed explanation of javascript function entry-level learning example code

Detailed explanation of javascript function entry-level learning example code

伊谢尔伦
伊谢尔伦Original
2017-07-25 13:32:281352browse

In the JavaScript world, functions are first-class citizens. They not only have all the ways of using traditional functions (declaration and calling), but also can assign values, pass parameters, and return like simple values. Such functions are also called First-class Function. Not only that, the function in JavaScript also acts as the constructor of the class and is an instance of the Function class. Such multiple identities make JavaScript functions very important.

Entry-level function

Like other languages, JavaScript functions follow the principle of declaration first and then use. Function names can only contain letters, numbers, underscores or $, and cannot start with a number. There are two common ways of declaring functions:

 // 直接声明函数myfunc 
 function myfunc(/* arguments */) { 
 } 
   
 // 把匿名函数赋值给本地变量myfunc 
 var myfunc = function(/* arguments */) { 
 }

Note that there are subtle differences between the above two ways of declaring functions: the first way is a named function when declared, whether it is declared before calling, After the call, even locations that will not be executed (such as after the return statement or in a branch that will never be true) are accessible in the entire scope; the second way is by assigning the anonymous function to a variable. , strictly speaking, this is not a function declaration but a function expression. This function cannot be accessed by any code before assignment, which means that the assignment must be completed before calling, otherwise An error occurs when calling: "TypeError: undefined is not a function". For example:

 myfunc1(); // 能够正常调用,因为myfunc1采用直接声明的方式 
   
 function myfunc1() { 
 } 
   
 myfunc2(); // 出错 TypeError: undefined is not a function 
   
 var myfunc2 = function() { 
 };

The basic calling method of the function is the same as that in traditional languages, using a pair of brackets: myfunc(). JavaScript functions also support direct or indirect recursive calls. For example, the classic Fibonacci function can be implemented in JavaScript like this:

 function fib(n) { 
   if (n == 1 || n == 2) { 
     return 1; 
   } else { 
     return fib(n - 2) + fib(n - 1); 
   } 
 }

Functions in JavaScript can handle variable-length parameters, and all parameters are processed inside the function. There is a local variable named arguments, which is an array-like object that contains all the parameters passed in during the call. The length attribute indicates the number of parameters. For example:

 function test() { 
   alert(arguments.length); 
 } 
   
 test(1); // 1 
 test(1, 'a'); // 2 
 test(true, [], {}); // 3 利用arguments可以实现类似C语言printf的功能,也可以用来实现方法的多态。

The above is the detailed content of Detailed explanation of javascript function entry-level learning example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn