简体   繁体   English

回报如何在JS幕后运作

[英]How return works in JS behind the scenes

I am new to JS and wanted to deeply understand how return in JS works. 我是JS的新手,想深入了解JS的收益是如何工作的。 For example, here are some code examples: let num = function(){return 5} and function number(){return 5} console.log(number()) . 例如,下面是一些代码示例: let num = function(){return 5}function number(){return 5} console.log(number()) The question is for example, in the second example Will number() which was passed to console.log() be replaced with the value that number() returns, that is, will console.log(number()) turn into console.log(5) behind the scenes. 问题是,例如,在第二个示例中,将传递给console.log()的number()替换为number()返回的值,即,console.log(number())会变成console。 log(5)在后台。 When we return something from a function then will that function be replaced with the value it returns. 当我们从某个函数返回某些内容时,该函数将被其返回的值替换。 Is it what happens behind the scenes when we return a value from a function? 当我们从函数中返回一个值时,它会在幕后发生吗?

both assign a name to a function, but one of them perform at run time, the other at compile time (or at begin of running), that's why if you call number it's known even if it defined below the call, but for the other, it should first get assigned. 两者都为一个函数分配了一个名称,但是其中一个在运行时执行,另一个在编译时(或在运行开始时)执行,这就是为什么如果您调用数字,即使它在调用下面定义,它也是已知的,而对于另一个,它应该首先被分配。

the return work the same in both, returning value 5. when you used () for calling the function. 当使用()调用函数时,两者的return工作相同,返回值5。

for more information on JS structure i suggest You don't Know JS book. 有关JS结构的更多信息,我建议You don't Know JS i'm not sure if it's allowed to suggest a book here, but it's free to read: You Don't Know JS 我不确定是否可以在这里建议一本书,但可以免费阅读: 您不懂JS

Update 更新资料

If by any change i got you wrong, and you are looking for a way to pass the actual function: 如果通过任何更改使我错了,并且您正在寻找一种传递实际功能的方法:

When you are calling num() or number() inside the console.log() like this: 当您在console.log()内部调用num()number() ,如下所示:

console.log(num()) 

It will process the 'num()' first, and then return the value which is '5', then that value, is passed to console.log(value) in here console.log(5) . 它将首先处理'num()',然后返回值为'5',然后将该值传递到console.log(5) console.log(value) console.log(5)

If you are looking for a way to pass the actual function/expression to the sub function, like what C# LINQ or Java Stream does, you need the secondary function to call the function it self. 如果您正在寻找一种将实际函数/表达式传递给子函数的方法,例如C# LINQJava Stream所做的事情,则需要辅助函数来自行调用该函数。 for example: 例如:

in here i wrote a function which filter out item of array, when user condition is true... Here condition is a function which passed to this function by user... and get called by secondary function... 在这里我写了一个函数,当用户条件为真时,它会过滤掉数组的项...这里条件是一个函数,该函数由用户传递给该函数...并被辅助函数调用...

function filter(list, condition) {
  let  result=[];
  for(let i = 0; i < list.length ; i++) {
    if(condition(list[i])) {
      result.push(list[i]);
    }
  }
  return result;
}

let condition = function(item) {
  return item.isOk;
}

and you call it like this: 您这样称呼它:

filter(myArray, condition)

Note: i used condition instead of 'condition()', now we passed the actual function instead of the value returned by that function 注意:我使用condition而不是'condition()',现在我们传递了实际函数,而不是该函数返回的值

In your first example you are assigning anonymous function yo a variable num . 在第一个示例中,您要为匿名函数yo分配一个变量num That is called function expression. 这就是所谓的函数表达式。 But function has not been executed, so the num will remain a function/object. 但是函数尚未执行,因此num仍为函数/对象。 To get the value from the function you should execute that with the parentheses num() . 要从函数中获取值,应在括号num()执行该值。 Or if you want immediately return value and assign that to a variable you should do like this: 或者,如果您想立即返回值并将其分配给变量,则应执行以下操作:

let num = (function () { return 5 })()

In your second example you first declare function and then in your console.log before showing on console you executes that number function and returns 5 and only after that your console.log will be executed and 5 will be passed as an argument. 在第二个示例中,首先声明函数,然后在console.log声明,然后在console.log显示之前,执行该number函数并返回5 ,只有在此之后, console.log将被执行,并将5作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM