javascript中return function与return function()的区别
参考https://stackoverflow.com/questions/7629891/functions-that-return-a-function-javascript
问题:唯一的区别是return中的函数是否带括号
输入:
function a() { alert('A!'); function b(){
alert('B!');
} return b();
} var s = a();
alert('break');
s();
输出:
A!
B!
break
输入:
function a() { alert('A!'); function b(){
alert('B!');
} return b;
} var s = a();
alert('break');
s();
输出:
A!
break
B!
回答1:
Assigning a variable to a function (without the parenthesis) copies the reference to the function. Putting the parenthesis at the end of a function name, calls the function, returning the functions return value.
http://jsfiddle.net/kaleb/Las6w/
function a() {
alert('A');// A未在此作用域定义
}
//alerts 'A', returns undefined function b() {
alert('B');
return a; //返回的是一个函数
}
//alerts 'B', returns function a function c() {
alert('C');
return a();//返回函数执行结果
}
//alerts 'C', alerts 'A', returns undefined alert("Function 'a' returns " + a());
alert("Function 'b' returns " + b());
alert("Function 'c' returns " + c());
回答2:
Returning the function name without ()
returns a reference to the function, which can be assigned as you've done with var s = a()
. s
now contains a reference to the function b()
, and calling s()
is functionally equivalent to calling b()
.
// Return a reference to the function b().
// In your example, the reference is assigned to var s
return b;
Calling the function with ()
in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();
, but instead of assigning the return value of b()
you are returning it from the calling function a()
. If the function b()
itself does not return a value, the call returns undefined
after whatever other work is done by b()
.
Returning the function name without () returns a reference to the function, which can be assigned as you've done with var s = a(). s now contains a reference to the function b(), and calling s() is functionally equivalent to calling b(). // Return a reference to the function b().
// In your example, the reference is assigned to var s
return b;
Calling the function with () in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();, but instead of assigning the return value of b() you are returning it from the calling function a(). If the function b() itself does not return a value, the call returns undefined after whatever other work is done by b(). // Execute function b() and return its value
return b();
// If b() has no return value, this is equivalent to calling b(), followed by
// return undefined;
回答3:
return b();
calls the function b(), and returns its result.
return b;
returns a reference to the function b, which you can store in a variable to call later.
javascript中return function与return function()的区别的更多相关文章
- javascript中的立即执行函数(function(){…})()
javascript中的立即执行函数(function(){…})() 深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是用(function(){…})()包 ...
- javascript中apply、call和bind的区别,容量理解,值得转!
a) javascript中apply.call和bind的区别:http://www.cnblogs.com/cosiray/p/4512969.html b) 深入浅出 妙用Javascrip ...
- JavaScript中var和this定义变量的区别
JavaScript中var和this定义变量的区别 在js中声明变量时可以使用var和this,但使用this的有很大一部分参考书是没有的,经过查阅相关资料总结如下: 用var和this声明变量,存 ...
- JavaScript中基本数据类型和引用数据类型的区别(栈——堆)
JavaScript中基本数据类型和引用数据类型的区别 1.基本数据类型和引用数据类型 ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型. 基本数据类型指的是简单的数据段,引用数据 ...
- javascript中三目运算符和if else有什么区别
javascript中三目运算符和if else有什么区别今天写了一个图片轮播的小demo,用到了判断先试了一下if else,代码如下:if(n >= count-1){n =0;}else{ ...
- javascript中back(-1)和go(-1)的区别
javascript中back(-1)和go(-1)的区别 一.总结 一句话总结: 数据 history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面 history.go(-1 ...
- 深入理解javascript中的立即执行函数(function(){…})()
投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-06-12 我要评论 这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是 ...
- Javascript中的感叹号和函数function
js函数前加分号和感叹号是什么意思?有什么用?:http://www.cnblogs.com/mq0036/p/4605255.html function与感叹号:https://swordair.c ...
- 【转】深入理解javascript中的立即执行函数(function(){…})()
javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...
- 理解javascript中的立即执行函数(function(){})()
之前看了好多代码,都有用到这种函数的写法,但是都没认真的去想为什么会这样写,今天开始想学习下jquery的源码,发现jquery也是使用这种方式,用(function(window, undefine ...
随机推荐
- S2750&S5700&S6700 V200R003(C00&C02&C10) MIB参考
https://support.huawei.com/enterprise/docinforeader.action?contentId=DOC1000027337&idPath=791971 ...
- #2 codeforces 480 Parcels
题意: 就是有一个用来堆放货物的板,承重力为S.现在有N件货物,每件货物有到达的时间,运走的时间,以及重量,承重,存放盈利.如果这件货物能再运达时间存放,并在指定时间取走的话,就能获得相应的盈利值.货 ...
- SQL存储过程使用参考代码
存储过程 use EBuy go --常用的系统存储过程 sp_addmessage --将新的用户定义错误消息存储在SQL Server数据库实例中 sp_helptext --显示用 ...
- 11.Django|中间件
中间件 文件夹为middlewareDemo 中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨 ...
- 018 spark on yarn (Job history)的配置,主要是yarn处跳转到历史聚合页面
一:目标 1.目标 在yarn的8080页面可以跳转到spark的日志18080页面. 因为在运行spark之后,看对应的job的日志,这样直接连接,更合理直接. 2.总结 在后面可以看到,其实不需要 ...
- C# 反编译防范
C# 编写的代码通过VS编译器生成 dll 或 exe ,很容易被一些反编译工具查看到源码或对源码进行修改.为防止代码被反编译或被篡改,我们可以进行一定的防范措施.但不能杜绝,因为DotNet编写代码 ...
- union的含义
用于合并两个或者多个select语句的结果集 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列的顺序必须相同. ex ...
- java中的instanceof用法详解
instanceof是Java的一个二元操作符(运算符),也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实例,返回的是boolean类型的数据.用它来判断某个对象是否是某个Clas ...
- Git中的bash与CMD的区别
Windows在使用git工具时,可以看到有两个命令输入窗: 1. Git CMD 2. Git Bash 两者的区别:Bash是基于CMD的,Bash在CMD的基础上新增了一些命令和功能,故建议使用 ...
- android studio java工程 报错
作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com android studio java工程 ...