javascript - 与返回语句混淆
问题描述
我正在尝试使用 return 语句调用或调用函数,首先我编写return
函数名,然后是括号,我们可以使用它return
来返回函数的引用,但是如果不是,我可以使用语句调用函数,return
那为什么?下面是我想要执行的代码
function make() {
var count = 0;
function counter() {
count = count + 1;
return count;
}
// here i ma calling a function with return statement return functionname followed by paranthesis
return counter()
}
var result = make();
console.log(result());
console.log(result())
console.log(result())
解决方案
您使用闭包count
,您需要将函数引用返回到内部函数。
稍后调用此函数并从返回实际计数counter
。
function make() {
function counter() {
count = count + 1;
return count;
}
var count = 0;
return counter;
}
var result = make();
console.log(result());
console.log(result());
console.log(result());
推荐阅读
- azure - 带有 SAML 签名证书的 Azure AD SSO
- python - 如何使用 while 或 for 循环替换列表中的部分字符串?
- ruby-on-rails - 如何在 Elasticsearch 中为 Rails 应用程序增加索引最大结果窗口?
- javascript - 混淆如何将此代码的变量放入 JS 中的堆栈和堆中
- java - 无法从 Office 365 门户打开我的应用程序。获取应用程序错误的未定义登录 url
- android - 如何删除清单合并错误
- javascript - 忽略 Vue 生产构建中的开发依赖
- powerapps - 如何在 powerapps 中打开文件或文档的 ac:// 文件夹
- elasticsearch - 执行汇总作业时映射异常
- excel - VBA 项目资源管理器中未显示表