了解 JavaScript 中现有对象的方法

Understanding methods of existing objects in JavaScript

我是 JavaScript 的新手,所以我正在阅读一些 JavaScript 代码(针对 Google Earth Engine)并且感到困惑。我有一些代码看起来像下面的伪代码:

function function_0(some_existing_object_from_elsewhere, further_argument) {

    // What happened to these computing since they are not included in the returned dictionnary?
    some_existing_object_from_elsewhere = some_function(some_existing_object_from_elsewhere)
    further_argument = further_argument === undefined ? 0 : further_argument

    // return a dictionnary of function
    return {
      function_1: function_1,
      function_2: function_2
    }
    
    function function_1(args) {
        some Code
        return something
    }

    function function_2(args) {
        some Code

        // call to function_0. This is the most confusing. How the call to function_0 can be explained here? 
        return function_0(some_existing_object_from_elsewhere, further_argument)
    }
}  

我的问题是:

  1. 这里返回的函数字典发生了什么变化?这些功能是否作为附加方法添加到 some_existing_object_from_elsewhere

  2. return之前的计算会发生什么,因为它们不包含在返回的字典中?

  3. function_0 的调用如何在 function_0 内部工作,而 function_0 实际上不是某种递归函数?那么,function_2returns、some_function(some_existing_object_from_elsewhere)返回的原始some_existing_object_from_elsewhere或处理后的some_existing_object_from_elsewhere是什么?

总的来说,我很困惑。任何指向进一步阅读的解释 and/or 都会有所帮助。请随意将您的解释扩展到我的问题中未涵盖的其他相关方面。

What happens to the dictionary of functions returned here? Are these functions added to some_existing_object_from_elsewhere as additional methods?

不是,返回函数的对象只包含对象中定义的函数:

return {
    function_1: function_1,
    function_2: function_2
}

但返回的对象 链接 到其他未返回的函数 function_1function_2 具有那些其他函数的标准词法范围,因为那些其他函数是在定义返回函数的同一块中定义的。

本质上,这意味着 function_0 返回的对象包含可以调用 其他函数 的函数,而无需直接将这些其他函数暴露给 function_0.

What happens to the computation before return since they are not included in the returned dictionary?

function_0 被调用的那一刻,什么都没有,真的 - 变量 some_existing_object_from_elsewherefurther_argument 只是变得可供 function_1 和 [=13 使用=],如果返回对象中的所述函数被调用过。

How does the call to function_0 works inside function_0 itself while function_0 isn't really some sort-of recursive function?

function_1function_2,因为它们最初定义在与 some_existing_object_from_elsewherefurther_argument 相同的块中,关闭那些变量。参见 closures。这意味着那些其他变量可供 function_1function_2 使用,直到这些函数被垃圾收集。

Then, what does function_2 returns, the original some_existing_object_from_elsewhere or the processed some_existing_object_from_elsewhere as returned by some_function(some_existing_object_from_elsewhere)?

新对象。这里:

function function_0(some_existing_object_from_elsewhere, further_argument) {
    some_existing_object_from_elsewhere = some_function(some_existing_object_from_elsewhere)
    further_argument = further_argument === undefined ? 0 : further_argument
    return {

两个参数都被重新分配给新值,然后返回一个对象,该对象关闭范围内的所有标识符。标识符引用新值。

您可能会发现浏览以下有关该主题的链接很有用:

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36#:~:text=In%20other%20words%2C%20a%20closure,created%2C%20at%20function%20creation%20time.&text=The%20inner%20function%20will%20have,the%20outer%20function%20has%20returned.

How do JavaScript closures work?

https://blog.logrocket.com/how-javascript-closures-work-in-plain-english/

快速回答:

  1. What happens to the dictionary of functions returned here? Are these functions added to some_existing_object_from_elsewhere as additional methods?

函数 return 是一个只有两个键的新字典,function_1 和 function_2。 some_existing_object_from_elsewhere不涉及return或被return修改。

  1. What happens to the computation before return since they are not included in the returned dictionary?

因为这些变量稍后会在 function_2 中使用,所以这些变量被包裹在一个“闭包”中,它保存这些变量供 function_2 稍后使用。
这意味着如果你 运行 function_0 两次使用不同的输入,然后 运行 returned function_2 两次,function_2 会有不同版本的 some_existing_object_from_elsewherefurther_argument.
闭包可能需要一些时间才能理解,因此我建议您查找更多相关资源。

  1. How does the call to function_0 works inside function_0 itself while function_0 isn't really some sort-of recursive function?

这里的关键是 function_0 不是在 function_0 内部调用,它是在新函数 function_2 内部调用,它被 returned 到调用范围, 因此调用范围可以 运行 它随时感觉它。
并不是说 JS 中有任何东西阻止递归函数。

Then, what does function_2 returns, the original some_existing_object_from_elsewhere or the processed some_existing_object_from_elsewhere as returned by some_function(some_existing_object_from_elsewhere)?

function_2 运行s function_0 和 returns 该函数的 运行 的输出。所以它也会 return 一个新的词典,其中包含 function_1function_2.
的新版本 在你的例子中,似乎 function_2 并没有真正做任何事情,所以我猜测在 Google Earth Engine 中对参数

进行了一些重要的修改