The this of the Apply invocation pattern in JavaScript points to

the first argument to apply specifies the direction of the this object in the function body. My previous understanding is:

A.apply (x array array); indicates that x calls function An and passes in the parameter array array, that is, similar to X.A (array [0], array [1] array), so it is very similar to the method call pattern that it is easy to understand that the this in function A points to x, that is, it specifies the direction of the this object in the function body.

here is my first question: the first parameter of apply specifies the direction of the this object in the function body. Is that what it means? but it is troublesome to understand it this way. It seems that it is better to bind the first parameter to the this of the function without thinking directly.

my second question is that I recently saw a piece of code:

var getSingle=function(fn){
    var ret;
    return function(){
        return ret || (ret=fn.apply(this,arguments));
    }
};
var getScript=getSingle(function(){
    return document.createElement("script");
});
var script1=getScript();
var script2=getScript();
alert(script1===script2);//true

var script3=window.document.createElement("script");
alert(script2===script3);//false???

this is my understanding of this code:

first check the related concepts: each function automatically takes two special variables when called: this and arguments. When an internal function searches for these two variables, it will only search for its active object, so cannot directly access these two variables in the external function in the closure .

executing var script1=getScript (); is equivalent to

var script1=getSingle(function(){
    return document.createElement("script");
})();

in this case, it belongs to the function call mode. The this in the, getSingle () function points to window, and then the closure in the getSingle function

return function(){
    return ret || (ret=fn.apply(this,arguments));
};

is called immediately at this time, so

in the closure
ret=fn.apply(this,arguments);

ret=(function(){
    return document.createElement("script");
}).apply(this,arguments)

the this in this apply also belongs to the function call mode because the closure is immediately called for execution, which in turn points to window.

and then here comes my second question , that is

but if, as I understood it before, it is similar to this strange gaygay thing

ret=~window.(function(arguments){
    return document.createElement("script");
})=document.createElement("script");

so my previous understanding is only suitable for the method invocation pattern. Can I just ignore . Apply (window,arguments) here? Is it bound to the global environment by default?

I added the last two lines of code myself, but the return is false? Then I was confused. Did I misunderstand the apply invocation pattern? Trouble God JS to help me solve the puzzle [holding fist]

< hr >

supplement on May 22, 2018

first of all, this code is a simple singleton mode , that is, use a variable to (ret) to mark whether an object has been created for a class. If so, the next time you get an instance of this class, (var script2=getScript ();) ,
directly returns the previously created object (ret=document.createElement ("script")) .
so script1 and script2 in the first alert are the same object, so true is returned. But the script3 I added myself is equivalent to creating a new variable with a different reference type than 1 and 2, so it must be false.

Mar.13,2021

you do not fully understand the reference types of js

console.log(document.createElement("script")===document.createElement("script"))

you run the above code and see the results
and

.
ret=fn.apply(this,arguments)

is equivalent to

ret=document.createElement("script");

or

ret=(function(){
    return document.createElement("script");
}).apply(this,arguments)
< hr >

ret= < del > window. < / del > (function (arguments) {
return document.createElement ("script");
}) = document.createElement ("script");

is that only when you access an undefined variable will you look for

on the window object.
Menu