Live data from Hacker News

“This” in JavaScript

bjorn.tipling.com

21–30 of 99 posts

Re: “This” in JavaScript

#21

This post is way, way too long. Rather than explaining the simple principles that lead to the behaviour JS's this has, the post just lists example after example. JS 'this' is a variable defined within a function. If that function is called as if it were a method, e.g. obj.somefunc(), 'this' is that object, obj. Otherwise, 'this' is the global object (quirks mode) or undefined (strict mode). If a function is called wi…

Your explanation is way simpler to understand than the lengthy original article.

Maybe one addition: the value of 'this' inside a function FN can also be set by a caller of FN by using the form FN.call(this-ptr-value, arg1, arg2, ..) instead of FN(arg1, arg2, ..)

Re: “This” in JavaScript

#22

The author critically misunderstands the way that `this` gets set, and in doing so has developed a significantly overcomplicated mental model. This is exemplified in the statement: You can use this in any function on an object to refer to other properties on that object. This is not the same as an instance created with new. ... Note, there was no use of new , no Object.create and no function called to create obj. Thi…

> The author critically misunderstands the way that `this` gets set...

I don't actually. (I'm the author) I know that the this key word is executed at run time to figure out what this is. It is true there are simpler ways to explain this and this blog post isn't that. I link to such a post at the bottom of my own. These are just some examples. There's also more to that blog post than what everyone is discussing, like returning from a constructor and global this in node.js and accessing this in eval and more. Anyway, I didn't submit this to HN and hadn't intended to. I also didn't intend to sit here tonight and put on a thick skin to deal with all this criticism for something I wrote months ago.

Re: “This” in JavaScript

#23
post #2

Not mentioned are ES6's arrow functions that capture the `this` value of the enclosing context. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

I think the new arrow functions are neat, but am I the only one who never really found it a hassle to throw a .bind(this) on a function?

Re: “This” in JavaScript

#24

The author critically misunderstands the way that `this` gets set, and in doing so has developed a significantly overcomplicated mental model. This is exemplified in the statement: You can use this in any function on an object to refer to other properties on that object. This is not the same as an instance created with new. ... Note, there was no use of new , no Object.create and no function called to create obj. Thi…

> The author critically misunderstands the way that `this` gets set... I don't actually. (I'm the author) I know that the this key word is executed at run time to figure out what this is. It is true there are simpler ways to explain this and this blog post isn't that. I link to such a post at the bottom of my own. These are just some examples. There's also more to that blog post than what everyone is discussing, like…

Don't take the criticism too hard. I enjoyed the post. I don't regularly write JavaScript so I found many points helpful. There is potential for pedantry here on HN, so dont take offense. I appreciate the post...

Re: “This” in JavaScript

#25
post #7

The title of this post alone fills me with anxiety and loathing. The only title I could think of that would be worse for a post is "Taxes you've been paying for years but didn't have to". (great essay BTW on a hated subject :-)

I'm pretty sure "taxes you haven't paid for years but should have" would be worse.

Re: “This” in JavaScript

#26

The author critically misunderstands the way that `this` gets set, and in doing so has developed a significantly overcomplicated mental model. This is exemplified in the statement: You can use this in any function on an object to refer to other properties on that object. This is not the same as an instance created with new. ... Note, there was no use of new , no Object.create and no function called to create obj. Thi…

> The author critically misunderstands the way that `this` gets set... I don't actually. (I'm the author) I know that the this key word is executed at run time to figure out what this is. It is true there are simpler ways to explain this and this blog post isn't that. I link to such a post at the bottom of my own. These are just some examples. There's also more to that blog post than what everyone is discussing, like…

The article was a nice refresher about 'this' subtleties, but it put too much work into contrasting it against Java's this, instead of just laying out the actual JS rules.

To me, 'this' is JS is a lot more self explanatory than this is Java. And your article doesn't make that apparent. In JS this is just another object, like any other object. It follows the exact same scoping rules as every other object.

Re: “This” in JavaScript

#27

Earlier quoted context omitted.

> The author critically misunderstands the way that `this` gets set... I don't actually. (I'm the author) I know that the this key word is executed at run time to figure out what this is. It is true there are simpler ways to explain this and this blog post isn't that. I link to such a post at the bottom of my own. These are just some examples. There's also more to that blog post than what everyone is discussing, like…

The article was a nice refresher about 'this' subtleties, but it put too much work into contrasting it against Java's this, instead of just laying out the actual JS rules. To me, 'this' is JS is a lot more self explanatory than this is Java. And your article doesn't make that apparent. In JS this is just another object, like any other object. It follows the exact same scoping rules as every other object.

>In JS this is just another object, like any other object.

You are mistaken, "this" is not an object nor a variable, it's a keyword and behaves differently than variable scope. Consider closures for example.

Re: “This” in JavaScript

#28
This is a pretty long article. The concepts you need to know aren't so long:

There are three ways to invoke a function. The meaning of `this` depends on which method was used:

- If a function is invoked in the form `foo.func()`, then the value of `this` is `foo`.

- If a function is invoked in the form `func()` (sometimes called "bare"), then the value of `this` is the "root object". In browsers the root object is `window`, in Node this it's the `global` object [0]. It's easier to make a bare invocation than you think:

    var bareFunc = foo.func;
    bareFunc();
- If a function is invoked via the `call()` or `apply()` function, then the value of `this` is whatever the invocation specifies. For example, in `func.call(foo)` the value of `this` will be `foo`.

If you ever pass a function to something else as a callback, you have no guarantee what the value of `this` will be when it is called. For example, if you register a callback for a click event, the value of `this` is whatever the caller wants it to be (in this case, it'll be the DOM element that's emitting the event):

    function MyFoo(elem) {
      elem.addEventListener('click', this.onClick);
    }

    MyFoo.prototype.onClick = function(e) {
      // `this` is whatever the caller wanted it to be
    }
You can force `this` to be a specific value by using the `bind()` function:

    function MyFoo(elem) {
      elem.addEventListener('click', this.onClick.bind(this));
    }

    MyFoo.prototype.onClick = function(e) {
      // `this` has been bound to what you think it should be.
    }
[0] However, if you're in strict mode, then in either case `this` will be `undefined`.

Re: “This” in JavaScript

#29
post #12

I skimmed the article. I began to get anxiety once I realized how long it was. JavaScript is a very simple language. `this` in JavaScript is easy to understand. Long posts like this give me anxiety.

I'm reasonably smart, but I don't ever remember the various contextual meanings of "this". Maybe because when I use javascript I try to use frameworks that hide the need to think about it. It's a wart on the language in my opinion.

Re: “This” in JavaScript

#30
post #28

This is a pretty long article. The concepts you need to know aren't so long: There are three ways to invoke a function. The meaning of `this` depends on which method was used: - If a function is invoked in the form `foo.func()`, then the value of `this` is `foo`. - If a function is invoked in the form `func()` (sometimes called "bare"), then the value of `this` is the "root object". In browsers the root object is `wi…

It's also worth noting that `bind` is not all that special; it simply returns a function that calls `apply` and passes the `this` object you specified.

I generally think of `this` as an invisible (sneaky) function parameter. It can be made more visible by using `call`:

    fn.call(object, param1, param2, ...);
Post reply on HN