anonymous functions
For what is an anonymous function, there is not much to be introduced here. What we need to know is that for JavaScript, anonymous functions are an important and logical feature. In general, the use of anonymous functions is to create a function for later use.
A simple example is as follows:
Window.onload = function () { alert ('hello');} var templateobj = { shout:function () { alert (' anonymous function as method ') ) }}templateobj.shout (); SetTimeout (function () { alert (' This is also an anonymous function ');}
One of the code snippets above I will not do too much useless explanation, more conventional.
Recursive
Recursion, plainly, is to call itself, or call another function, but the function of the call tree somewhere in the call itself. So the recursion, it was produced.
Recursion for common named functions
The best example of recursion for a generic named function is to use the simplest recursive requirement: to detect a palindrome.
A palindrome is defined as follows: a phrase, regardless of the direction in which it is read, is the same. The work of testing is of course diverse, and we can create a function that generates a character in reverse order with the back character to be detected, and then detects whether the two are the same, or, if it is the same, a literal character.
But this method is not very strong, to be exact, the price is relatively large, because we need to allocate and create new characters.
So, we can sort out the following simple ways:
- Single and 0 characters are palindrome
- If the first and last characters of the string are the same, and besides two characters, the other character satisfies the requirement, then we can detect it. This is a palindrome.
function Ispalindrome (txt) { if(txt.length<=1) { return true ; } if (Txt.charat (0)! = Txt.charat (txt.length-1returnfalse; return ispalindrome (txt.substr (1, txt.length-2));}
1190000011792660
In-depth understanding of function operations in JavaScript