Still can't understand closures?

Today"s interview knelt on the closure again, but I still can"t really understand the closure.
read teacher Ruan Yifeng"s understanding of closures and finished the thinking questions, and then added the difficulty himself. The code is as follows:

    var name   = "The Window";
    var object = {
        name       : "My Object",
        getNameFunc: function () {
            return function () {
                return this.name;
            }
        }

    };
    var obj    = {
        name   : "wgm",
        sayName: function (callback) {
            callback();
        }
    }

    console.log(
        obj.sayName(object.getNameFunc())//undefined
    );

Why is it printed as undefined

Apr.01,2022

return this.name;
where this points to the function return function () {} instead of object , so this.name is undefined.
like this:
 var object = {
    name: "My Object",
    getNameFunc: function () {
        return function () {
            return object.name;
        }
    }
};
undefined

there is nothing wrong with the whole closure, in

return function () {
  return this.name;
}
The this obtained in the

closure function is really window , which you can print at this layer.
on the contrary, there is a relatively simple place where the landlord missed it. Clearly, the content of console is the return value of obj.sayName (...) , but does this function have a return value?

sayName: function (callback) {
  callback();
}

callback () after executing the data returned by return , there are no variables to accept and are not returned by the sayName function, so the result is undefined . If you want to get the correct result, the landlord can slightly modify it to return callback (); .

in addition, I will briefly explain some concepts related to closures, hoping to help you deepen your understanding of closures. The chained scope of
JS is characterized by that the function execution enters from outside to inside, and returns from the inside out, so the inner function can get the variables defined in the outer function when it is executed, but when the outer function returns, the inner function has been destroyed in memory after execution, so the variables defined by the inner function scope cannot be obtained, that is, the variables cannot be obtained from the lower layer. What if the outer function sometimes needs to get the variables of the inner function? So the closure appears, and in order to solve the problem that it is impossible to get variables from the inner function, define another function in the inner function to get its variables back to the outer function, which is a bit like digging a new hole to fill an old hole, such as the following code:

function a () {
  var aa = 'aa', cc;
  function b () {
    var bb = 'bb'
    return function c () {
      return bb
    }()
  }
  cc = b()
  console.log(cc)
}
a()

if there is no c function, the a function cannot get the bb in the b function. So the use of closures exists to get the variables of the inner function, and closures are not needed when there is no such requirement.

Menu