-2

I know you can change the context of this by using apply and etc.

But why can't you do this?

this = 5; // invalid left-hand side in assignment

It's not much different from this:

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

Foo.apply(5);

What is the difference between using apply and this = 5? Doesn't apply just locally redefine what this is for whatever function is being called?

anonymous
  • 33
  • 7
  • First, you don't need to--just define another variable, initialize it to `this`, and then change it to your heart's content. Second, it would make it pretty much impossible to figure out what a program was doing. –  May 11 '16 at 02:42
  • Google for "change this javascript". –  May 11 '16 at 02:46

4 Answers4

1

You cannot redefine reserved keywords. this is one of them.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

ryanpcmcquen
  • 5,446
  • 3
  • 20
  • 34
  • But how is using `apply` not just locally redefining `this` for whatever function is being called? – anonymous May 11 '16 at 02:41
  • `apply` gives a new context that is `this`, it does not redefine it. – ryanpcmcquen May 11 '16 at 02:42
  • 2
    @anonymous A function **has no** `this` until it is called. Therefore, specifying the `this` with `apply` etc. is not redefining it, it's specifying it, and it's not doing it "locally", which would mean within the function, it's doing it at the point of call. –  May 11 '16 at 02:51
1

this is a reserved word, not a normal variable.

Online Sid
  • 116
  • 4
1

this is the context you are in. You cant change it because to change the context you simply need to be in a différent context ...

In the same raison you cant change me in vb or this in c#.

'this' is not an assignable variable. It's read-only.

maybe with a real code where you need to change the this i can suggest you something else ...

you can do something like this:

var myFunction = function(arg1) {
   //Do something here ...
};

this if you need to change it, just reassing the varriable:`

myFunction = function(arg1) {
    //Do something else here ...
};
Benoit
  • 949
  • 9
  • 26
  • "*this is the context you are in*" absolutely not. An [*execution context*](http://www.ecma-international.org/ecma-262/6.0/#sec-execution-contexts) has a *this* parameter that can be set to reference **any** object in non–strict mode, or **any** value in strict mode. You can't reference an execution context at all, and the value of its *this* can be set to a different value on every call (see [*§8.3.3 ResolveThisBinding ( )*](http://www.ecma-international.org/ecma-262/6.0/#sec-resolvethisbinding)). – RobG May 11 '16 at 02:58
0

This is an object. You could assign a numerical value to a property of the this object, but you cannot assign a numerical value to this itself.

Gregory Ling
  • 132
  • 11
  • What is the child of an object? –  May 11 '16 at 02:42
  • such as this.hello. hello is a child of this. – Gregory Ling May 11 '16 at 02:43
  • That is a property, not a child. –  May 11 '16 at 02:43
  • A child in this context refers to a piece of an object. Say we have an object foo. In the object foo, there is nothing. Now we add a child bar. Foo now has a variable bar inside it which can be referenced from 'foo.bar'. – Gregory Ling May 11 '16 at 02:45
  • sorry, wrong wording... you're right... I didn't know what other word to use. – Gregory Ling May 11 '16 at 02:46
  • It's neither a "child", nor a "variable". It's called a "property". Let us use proper terminology. –  May 11 '16 at 02:47
  • Note that in strict mode, *this* can have any value (including primitives and *null*). Only in non–strict mode must it be an Object. – RobG May 11 '16 at 02:55