Home > front end >  About js this problem
About js this problem

Time:12-15

<script>
Var name='111'
Var a={
Name: '222'
Say: the function () {
The console. The log (enclosing name)
}
}
Var b={
Name: '333'
Say: function (fun) {
Fun ()
}
}
B. say (a. say)
</script>
Or
<script>
Var name='111'
Var obj={
Name: '222'
Say: the function () {
(function () {
The console. The log (enclosing name)
} ())
}
}
Obj. Say ()
</script>
Why is the console window is this?

CodePudding user response:

This generally refers to the object, of the calls to functions that contain this here this should not mean b. say methods or contain anonymous functions ()? But these are not objects

CodePudding user response:

Inside the method, this is a special variable, it always point to the current object, you this here refers to the object a

https://shuxhan.com/20201104-javascript-function/#1.this

CodePudding user response:

Want to method invokes the object's properties, must go through this

CodePudding user response:

reference 1/f, Where am I in heart response:
this generally refers to the object of the calls to functions that contain this, here, this should not mean b. say methods or contain anonymous functions ()? But these are not objects

Wrong, if it is a direct call b.s ay (), that this is the point b, but you are the a.s ay as a callback, equivalent to
Const say=a.s ay;
B.s ay (say);
And you are outside you define a global function is the same effect, its internal this point is the window

This this is decided by the execution context, not to say that the function definition is what it is what appearance, the place you are going to the function call, is determined by the way it is called
  • Related