When a function is called without an object(nothing on the left) does keyword this bound to the global object by default or it depends on the call site of the function getting called not always the global object?
Asked
Active
Viewed 17 times
0
-
When you don't call it on an object, the `this` binding is `undefined`. Only in sloppy mode functions, that defaults to the global object. (Strict mode depends on the definition site of the function, not on the call site) – Bergi Oct 31 '20 at 06:36
-
iam reading an article saying that: ''The place where a function is invoked in a JavaScript program is called the call-site. This is also important in determining the value of this because if the function isn’t invoked by any visible object, the current context of the call-site would be responsible for setting the new execution context of the function''. so what he is saying here is wrong it will be always refer to global (in non-strict mode) ? or what he is saying is right but i am missing something ? – Muhammad Hossam Oct 31 '20 at 06:57
-
It's unclear what "*the current context of the call-site*" means there. Can you link the whole article? But in general, yes it seems wrong, when a function isn't called from a method reference, the call site passes `undefined`. (That "*isn't invoked by any visible object*" might have same edge case in `with` scopes). – Bergi Oct 31 '20 at 15:37