-2

function log(this) {
  console.log(this);
}

It throws the error Unexpected token this. So why doesn't JavaScript accept this as an argument?

Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
ThisisFish
  • 416
  • 1
  • 6
  • 14
  • 5
    `this` is a reserved keyword in javascript.It cannot be used as function argument but can be used inside a function and its value depend on how the function is called – brk Feb 27 '18 at 16:46
  • 1
    For a further explain of `this` keyword, read [How does the “this” keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Wondercricket Feb 27 '18 at 16:47
  • is their any way to override this rule. – ThisisFish Feb 27 '18 at 16:47
  • 2
    No. In any given language there are usually certain keywords you can't use, in JavaScript you can't use `this` in this way. You also couldn't use `function` for example. – James Thorpe Feb 27 '18 at 16:48

2 Answers2

2

this is a reserved keyword so it can't be used as a variable name.

If you want to override the this value for a function, you can use call or apply.

function log() {
   console.log(this);
} 

log.apply({ custom: "this value" });
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

this has special meaning in the language. It's an identifier, but can only be defined automatically in specific situations, never explicitly by the developer.

Other identifiers that are automatically defined can still be defined by the developer, like arguments, undefined and window, though it should be avoided in most cases.


To add clarity, in programming an identifier is a label used by a programmer to reference a value.

In JS, this is indeed a keyword, which, according to ECMAScript semantics, prevents it from being explicitly declared as an identifier. This does not mean that it isn't an identifier at all, which it clearly is since it will always let the programmer reference a value.

So something being a keyword does not mean that it isn't an identifier. It does mean though that in JS, you don't have the option of explicitly declaring an identifier with that name, though other languages do sometimes allow this.

  • Actually it's *not* an identifier. It's a reserved keyword. – Bergi Feb 27 '18 at 16:50
  • @Bergi: To not acknowledge its role as an identifier would be to engage in pedantic quibbles over semantics. In use, it is as much as a variable identifier as any other, but is one that we can't explicitly declare. You're right that it's not in the sense that we're prevented from declaring it as a variable, but you're wrong in the sense that we're *not* prevented from declaring it as an object property identifier. In practice, it is a variable identifier, very similar to a function parameter, but with restrictions. –  Feb 27 '18 at 16:54
  • ...it being a keyword doesn't automatically mean it would be impossible to be also declared as a variable identifier. It all comes down to the language semantics. Sometimes keywords in languages are permitted to be declared as vars, if the semantics permit it. –  Feb 27 '18 at 16:56
  • Sure it comes down to semantics - the JS semantics specifically, which do not allow using keywords as variable identifiers. That's exactly the reason why the OP is getting a syntax error, so it's actually quite important imo. (And yes, it's a valid *identifier name*, so e.g. valid as an object property key even without quotes, but that doesn't matter here). – Bergi Feb 27 '18 at 17:09
  • @Bergi: The semantics do not allow keywords to be *declared* as variable identifiers. This doesn't mean that the `this` keyword isn't *used* as an automatically declared identifier, which it very obviously is. –  Feb 27 '18 at 17:12
  • The point of bringing up object properties is that people speak as though there's some universal law that dictates a language keyword must never be an identifier. There is much more thought and consideration to be put into such things. –  Feb 27 '18 at 17:13
  • No, `this` is not an identifier, it's an extra kind of expression. It's more like `null` or `true` than an identifier expression. (And btw, yes, there is a universal law for this: [the ECMAScript specification](http://www.ecma-international.org/ecma-262/#sec-names-and-keywords)) – Bergi Feb 27 '18 at 17:14
  • @Bergi: It is vastly *less* like `null` or `true` *in its use* as those identifiers always refer to a single, internal value. The `this` can reference *any* value. There is far less distinction between `this` and a declared variable; mostly it's just a `const` that can only be automatically declared. –  Feb 27 '18 at 17:16
  • @Bergi: That's not a universal law. That law only governs ECMAScript. Other languages allow keywords as vars in some cases. –  Feb 27 '18 at 17:17
  • 1
    https://en.wikipedia.org/wiki/Identifier Your understanding of the word "identifier" is hyper-focused on a specific text, and not on what is understood to be an identifier in general in programming, which is a label that references data, which is exactly what `this` is. –  Feb 27 '18 at 17:20
  • 1
    Even Wikipedia says "*Which character sequences constitute identifiers depends on the lexical grammar of the language.*" And this question is about JavaScript, so please allow me to be focused on that context. I'm not denying that "this" can be an identifier in the generic meaning. – Bergi Feb 27 '18 at 17:23
  • 1
    @Bergi: Yes, some characters sequences may not be used at all as identifiers. For example, `@#!` is a character sequence that will never refer to any value in JS, but could in another language. However, `this` does refer to a value, and in fact it always does. It's important to understand what an identifier *is* before we worry about semantic restrictions on their declarations. –  Feb 27 '18 at 17:28
  • @Bergi: I added some text to my answer to make the distinction clearer. –  Feb 27 '18 at 17:35