When to use return, and what happens to returned data?(何时使用 return,返回的数据会发生什么?)
问题描述
有什么区别:
function bla1(x){console.log(x)}
和
function bla(x){return console.log(x)}
在什么情况下应该使用return?
In which cases should I use return?
另外,当一个函数返回一个值时,它会发生什么?它存储在某个地方吗?
also, when a value is returned from a function, what happens to it? is it stored somewhere?
推荐答案
有什么区别
第一个函数返回 undefined(因为它没有明确地 return 任何内容),第二个函数返回 console.log 返回的任何内容.
The first function returns undefined (as it does not return anything explicitly), the second one returns whatever console.log returns.
什么情况下应该使用return?
In which cases should I use return?
当函数正在生成某个值并且您想将其传递回调用者时.以 Math.pow 为例.它接受两个参数,基数和指数,并将基数返回到指数.
When the function is generating some value and you want to pass it back to the caller. Take Math.pow for example. It takes two arguments, the base and the exponent and returns the base raised to the exponent.
当一个值从一个函数返回时,它会发生什么?它存储在某个地方吗?
When a value is returned from a function, what happens to it? is it stored somewhere?
如果要存储返回值,则必须将其分配给变量
If you want to store the return value, then you have to assign it to a variable
var value = someFunction();
这会将 someFunction 的返回值存储在 value 中.如果您在没有分配返回值的情况下调用该函数,则该值将被静默删除:
This stores the return value of someFunction in value. If you call the function without assigning the return value, then the value is just silently dropped:
someFunction();
<小时>
这些是编程基础知识,不仅与 JavaScript 相关.你应该找到一本介绍这些基础知识的书,尤其是关于 JavaScript 的书,我建议阅读 MDN JavaScript 指南.也许关于 函数 的维基百科文章也有帮助.
These are programming basics and are not only relevant to JavaScript. You should find a book which introduces these basics and in particular for JavaScript, I recommend to read the MDN JavaScript Guide. Maybe the Wikipedia article about Functions is helpful as well.
这篇关于何时使用 return,返回的数据会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:何时使用 return,返回的数据会发生什么?
基础教程推荐
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
