简体   繁体   中英

Understanding methods of existing objects in JavaScript

I am new to JavaScript, so I am reading some JavaScript code (for Google Earth Engine) and I got confused. I have some code that looks like the following pseudo code:

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)
    }
}  

My questions are:

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

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

  3. How does the call to function_0 works inside function_0 itself while function_0 isn't really some sort-of recursive function? 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) ?

Overall, I am very confused. Any explanation and/or points to further reading would be helpful. Feel free to extend your explanations to other related aspects not captured in my questions.

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

Not really, the object of returned functions only contains the functions defined in the object:

return {
    function_1: function_1,
    function_2: function_2
}

But the returned object is linked to the other functions not returned in that function_1 and function_2 have standard lexical scope of those other functions, because those other functions were defined in the same block that the returned functions were defined.

Essentially, what this means is that the object returned by function_0 contains functions that can call other functions , without directly exposing those other functions to the caller of function_0 .

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

At the moment that function_0 is called, nothing, really - the variables some_existing_object_from_elsewhere and further_argument simply become available to be used by function_1 and function_2 , if said functions in the retuned object are ever called.

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

The function_1 and function_2 , since they're initially defined in the same block as some_existing_object_from_elsewhere and further_argument , close over those variables. See closures . It means that those other variables become available for use by function_1 and function_2 until those functions become garbage-colleced.

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)?

The new objects. Here:

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 {

Both arguments get reassigned to new values, and then an object is returned which closes over all identifiers in scope. The identifiers reference the new values.

You may find it useful to browse the following links on the subject:

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/

Quick answers:

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

The function returns a new dictonary with only two keys, function_1 and function_2. some_existing_object_from_elsewhere is not involved in the the return or modified by the return.

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

Because these variables are used later in function_2 , these variables become wrapped in a "closure", which saves these for use by function_2 later.
That means if you ran function_0 twice with different inputs, and then ran the returned function_2 for both of them, both executions of function_2 would have different versions of some_existing_object_from_elsewhere and further_argument .
Closures can take some time to understand so I recommend looking up further resources on this.

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

The key here is that function_0 is not being called inside function_0 , it's being called inside the new function function_2 which is returned to the calling scope, and so the calling scope is allowed to run it whenever it feels like it.
Not that there's anything preventing recursive functions in 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 runs function_0 and returns the output of that run of that function. So it will also return a new dictionary with new versions of function_1 and function_2 .
In your example, it seems like function_2 doesn't really do anything, so I'm guessing in Google Earth Engine there's some important modification to the arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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