What are the possible reasons for passing "global objects defined in a function" as arguments?

Asked 2 years ago, Updated 2 years ago, 20 views

There was a code that passed the "global object defined in the function" as an argument. What is the possible reason?
"·Why is it not ""window"" or ""this""?"
"· Am I correct in understanding that ""global objects"" need not be passed as arguments basically?"
"·When, for example, do you have to pass the ""global object"" as an argument (or it is better to give it?"
·When straddling multiple files?

function f1(){
  obj = {
    p —"Value"
  }
  f2(obj);
}

javascript

2022-09-29 22:14

1 Answers

It may be a bit confusing, but I will check the terms first.

Generally , JavaScript refers to the Global Object, or the only object that corresponds to the widest variable scope in the language system.The implementation of this object itself is considered implementation-dependent, so each processing system behaves differently.For example, it is implemented as an object that can be accessed under the name window for browsers and global for nodes.In any case, the meaning of the scope being global means anywhere accessible as we normally understand.Certainly, you can access windows from anywhere.

The "global object" in the question to corresponds to the global object, the object at the global scope, or the global variable if you pick up a synonym in another language.This is defined as the property of the Global Object in the preceding paragraph, so it refers to the same scope as the Global Object, the variable that can be accessed from anywhere.In that sense, there is also a context called a "global object."

As mentioned above, global and global objects are objects defined at a scope that can be accessed from anywhere.You can see how to pass this to a function after all.

Global objects need not be passed as arguments

It is also possible to view thatHowever, if you have used jQuery, for example, you should at least experience the meaning of passing a global object or global object to a function.

//$=jQuery:function

// to the window object (global object)
// Generate jQuery object for
const$window=$(window);

// to the document object (global object; window.document)
// Generate jQuery object for
const$document=$(document).ready(...)

In this way, passing a global object or global object to a function is not special, unusual, or unnatural.

"·When, for example, do you have to pass the ""global object"" as an argument (or it is better to give it?"
·When straddling multiple files?

It's a little unnatural to say that you have to pass it, but if you're writing with a closure in a library implementation, of course you'll need to refer to a global object or a global object in order to make the library available from the outside.This may not be a very good example.

// Lock library implementation into a closure
(function(root){
    // Local variables are hidden in local scope
    const privateObject = {
        publicProperty: 0,
    };

    // Exporting Library Functions
    root.library={
        getPublicProperty:() = > privateObject.publicProperty,
    };
}) (this);
// The object to be exported can be viewed from anywhere.
// Global Object or Global Object

When you actually implement something, I think it's almost unnecessary to worry about whether it's a global object or a global scope (I probably haven't designed it).On the other hand, since at least pure js uses a duck-typing format, verifying that the passed object is appropriate is more substantially important.Or, if I were to pass it, would I not pass it to an unexpected action (for example, an extension method that performs destructive operations on an object)?

The above is not a direct answer, so please refer to it only.


2022-09-29 22:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.