JS Basic Tutorial: Learn JavaScript Anonymous Functions
There is no doubt that John Resig is a meticulous and thoughtful person. With his careful study of the anonymous functions we usually use, he can also dig out some new things. Usually, recursion occurs when a function calls itself. We are not unfamiliar with function calls like the following.
1.function yell(n){
2. return n > 0 ? yell(n-1) + "a" : "hiy";
3.}
4.alert( yell(4))// The result is: hiyaaaa;
There is no problem with a single function. What will happen if we use an anonymous function and place it inside an object?
1.var ninja = {
2. yell: function(n){
3. .};
6.alert(yell(4))//The result is: hiyaaaa;
Now we don’t see any problem. If we create a new object and copy the yell method from ninja, the situation will be different. It’s different. Since the anonymous function is inside ninja, this method is still a reference to the yell method of the ninja object. The problem arises if we redefine the ninja object.
02. yell: function(n){
03.
05.};
06.var samurai = { yell: ninja.yell };
07.var ninja = {};
08.try {
09. alert(samurai.yell(4);
10.} catch( e){
11. alert("Uh, this isn't good! Where'd ninja.yell go?" );
12.}
13.//The result is: "Uh, this isn't good! Where 'd ninja.yell go?"
How to solve this problem? How to make the yell method more reliable? The most common way is to use "this" inside the ninja.yell method to mutate all instances of the ninja object, that is:
2. Yell: Function (n) {
3. Return n & gt; 0? This.yll (n-) + "a": "hiy";4.
5.};
Now we test and we will get the results we need. This is of course one way, the other way is to name the anonymous function, which seems contradictory, but it does work well, voila:
01 .var ninja = {
02. Yell: Function Yell (n) {03. Return n & gt; 0? Yell (n-) + "a": "hiy";
04.
05. 06.alert((ninja.yell(4)) + " Works as we would expect it to!" );
07.var samurai = { yell: ninja.yell };
08.var ninja = {};
09 .alert( (samurai.yell(4))+ " The correctly method calls itself." );
Naming anonymous functions can go a step further. For normal variable declarations, we can also try to do this, such as:
2. alert( (ninja == myNinja) + " This function is named two things - at once!" );
3.};4.ninja();
Run the above function. In IE, what we see is: "flase This function is named two things – at once!". In FF, what we see is: "true This function is named two things. – at once!”. The author once pointed out: Anonymous functions can be named, but they are only visible within the function itself. It seems that is not the case. The test results show that for IE, it is not visible, but in FF, the result is as the author expected. At the same time, we tested myNinja, and the results were also different in IE and FF.
2.//It is "undefinde" in FF
3.//It is "function" in IE
In this way, naming the anonymous function, in IE, Only visible externally; in FF, only visible inside functions. In fact, we can use arguments.callee to get the results we need, as follows:
1.var ninja = {
2. .};
6.alert( ninja.yell(4));
Arguments.callee can be used for every function. It provides us with a reliable method to access the function itself. I think this method is relatively simple and reliable.

作为一门现代化的编程语言,Golang(又称Go语言)具有众多强大的特性。其中,匿名函数是Golang的一个非常重要的概念,被广泛应用于各种场景中。在本文中,我们将深入分析Golang函数中匿名函数的应用场景。事件处理器在事件处理器中,匿名函数是一个非常方便和实用的工具。可以通过匿名函数向事件处理器传递自定义的逻辑,比如:funcmain(){bt

pythonLambda表达式是一个强大且灵活的工具,可用于创建简洁、可读且易于使用的代码。它们非常适合快速创建匿名函数,这些函数可以作为参数传递给其他函数或存储在变量中。Lambda表达式的基本语法如下:lambdaarguments:expression例如,以下Lambda表达式将两个数字相加:lambdax,y:x+y这个Lambda表达式可以传递给另一个函数作为参数,如下所示:defsum(x,y):returnx+yresult=sum(lambdax,y:x+y,1,2)在这个例子

如何利用PHP7的匿名函数和闭包实现更加灵活的代码逻辑处理?在PHP7之前,我们经常使用函数来封装一段特定的逻辑,然后在代码中调用这些函数来实现特定的功能。然而,有时候我们可能需要在代码中定义一些临时的逻辑块,这些逻辑块没有必要创建一个独立的函数,同时又不想在代码中引入太多的全局变量。PHP7引入了匿名函数和闭包,可以很好地解决这个问题。匿名函数是一种没有名

PHP8.0是当前最新版本的PHP编程语言。一项重要的更新是对匿名函数的改进和增强。匿名函数(也称为闭包)是一种特殊类型的函数,可以在运行时动态创建并传递给其他函数或存储在变量中。在PHP中,匿名函数对于高级编程和Web开发至关重要。PHP8.0提供了一些新的语法和功能,可以使匿名函数更加灵活和易于使用。其中一些更新如下:函数参数的类型声明在PHP8.0中,

pythonLambda表达式是一个小的匿名函数,它可以将一个表达式存储在变量中并返回它的值。Lambda表达式通常用于执行简单的任务,这些任务可以通过编写一个单独的函数来完成,但Lambda表达式可以使代码更简洁和易读。Lambda表达式的语法如下:lambdaarguments:expressionarguments是Lambda表达式接收的参数列表,expression是Lambda表达式的体,它包含需要执行的代码。例如,以下Lambda表达式将两个数字相加并返回它们的和:lambdax,

python中的Lambda表达式是匿名函数的另一种语法形式。它是一个小型匿名函数,可以在程序中任何地方定义。Lambda表达式由一个参数列表和一个表达式组成,表达式可以是任何有效的Python表达式。Lambda表达式的语法如下:lambdaargument_list:expression例如,下面的Lambda表达式返回两个数字的和:lambdax,y:x+y这个Lambda表达式可以传递给其他函数,例如map()函数:numbers=[1,2,3,4,5]result=map(lambda

匿名函数顾名思义指的是没有名字的函数,在实际开发中使用的频率非常高,也是学好JS的重点。下面本篇文章就来给大家详细介绍一下JavaScript中的匿名函数,希望对大家有所帮助!

如何使用PHP7的匿名函数和闭包实现更加灵活和可复用的代码逻辑?在PHP编程领域中,匿名函数和闭包是非常有价值和强大的工具。PHP7引入了一些新的语言特性,使得使用匿名函数和闭包更加方便和灵活。本文将介绍如何使用PHP7的匿名函数和闭包来实现更加灵活和可复用的代码逻辑,并提供一些具体的代码示例。一、匿名函数匿名函数是一种没有名称的函数。在PHP中,可以将匿名


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
