In-depth understanding of function operations in JavaScript

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.