>>> function a(){function b() {return "aaa"} Function.prototype.c=function(){return b();}}
>>> a()
>>> a.c
function( )
>>> a.c()
"aaa"
>>> a.hasOwnProperty("c")
false
See This code first declares a function a, and defines a function b internally. However, function b is not a method of function object a, but is just a temporary variable function (or private function, I don’t know how to describe it) in the function a block. Later, a function c was defined inside a using function(){}, so a closure was generated, so c could traverse all the internal variables of the block under a, including b of course. I linked c under Function.prototype. , that is, it is not directly linked to a, but linked to the prototype chain of a. Finally, it is executed, and hasOwnProperty is also false, and there is
code
>>> d={}; function a(){function b(){return "aaa"} d.c=function(){return b();}}
>>> a()
>>> d.c ()
"aaa"
closure has nothing to do with the context of function execution. Context can use the call apply method to change this, but there seems to be no way to modify the closure after the function is defined. Yeah, I don’t know if that’s the case
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn