6

I read the following on a website:

Use-strict has an advantage. It eliminates this coercion. Without strict mode, a reference to a this value of null or undefined is automatically coerced to the global. This can cause many headfakes and pull-out-your-hair kind of bugs. In strict mode, referencing a a this value of null or undefined throws an error.

What exactly does this mean? What does use-strict have to do with this coercion?

intcreator
  • 4,206
  • 4
  • 21
  • 39
Sunny Bharadwaj
  • 153
  • 1
  • 11
  • 2
    What do you already know about Javascript's `this`? (Would an explanation have to start with the basics of what `this` is used for in a general sense, or...?) – nnnnnn Feb 28 '16 at 04:29
  • 3
    The text you quoted seems to explain what "this coercion" means (in this context). What exactly do you not understand? The description is not very precise: *" In strict mode, referencing a a this value of null or undefined throws an error."* That's not true of course. Accessing `this` itself won't throw an error. `this.something` would. – Felix Kling Feb 28 '16 at 04:36
  • 1
    This [link](http://stackoverflow.com/a/11496488/452708) might throw some insights. – Abhijeet Feb 29 '16 at 02:56
  • Possible duplicate of [Why does using \`this\` within function give me a "Possible strict violation" in jshint?](http://stackoverflow.com/questions/23146955/why-does-using-this-within-function-give-me-a-possible-strict-violation-in-j) – Paul Sweatte Feb 10 '17 at 03:31
  • haha i am looking at the same video where this line was stated – suku Mar 12 '17 at 08:20

1 Answers1

9

When you call a function in javascript, 'this' will refer to different things depending on the context:

  1. If the function has been bound, the 'this' will be set to whatever it was bound to, e.g. fn.bind(x)()

  2. If you invoked the function using fn.call(x) or fn.apply(x), the this will be set to x.

  3. If the function was defined using arrow notation, then the this will be whatever was defined to be this when the function was defined.

  4. If you call the function with thing.fn(), the this is what is before the '.', in this case 'thing'.

  5. If you're in a constructor, called with new then this refers to the new object under construction.

  6. If you are just calling a bare function, that isn't on any object, that isn't bound, that isn't an arrow function and you're calling it in the straightforward way, without using call or apply, then the this will refer to the global object if you are not in strict mode, and undefined if you are in strict mode. This is what is referred to as 'this coercion' by the quote.

That's why, if you open a browser console and type

Function('console.log(this)')()

the console will output the Window which is the global object in the browser. However if you open the console and type

Function('"use strict";console.log(this)')()

the console will log undefined.

I'm using the Function constructor here, because it's a way to force the use of non-strict mode regardless of the situation it appears in - so these examples should still work, even if you run them from inside a file or console operating in strict mode.

this coercion can be the the most convenient way of getting the global object, i.e.

const global = Function('return this')()

works in both the browser and node, even in strict mode.

But most of the time, you want to fail fast, and having functions that you expected to operate on specific kinds of instances actually operate on your global object can mess things up badly. Having attempts to write something to or read something from this throw exceptions when it isn't defined is almost always better than reading and writing to the global object.

kybernetikos
  • 8,281
  • 1
  • 46
  • 54