Live data from Hacker News

“This” in JavaScript

bjorn.tipling.com

1–10 of 99 posts

Re: “This” in JavaScript

#3
There's something to be said about the distinction between "this" and "self" as names for the self-referential instance object. While "self" emphasizes mutability and introspection, "this" evokes a more material, evidentiary abstraction.

When I transitioned from PHP to Python, the change in nomenclature (albeit only a social norm in Python) was a big part of what helped me think bigger about this concept.

To this point, this read might have been easier to digest had it started with the section "object this."

Re: “This” in JavaScript

#4
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...

TypeScript has these and they're marvelous. They make writing functional style code very enjoyable and not having to capture `this` yourself is alone justification for this developer to never go back to pure JavaScript.

Re: “This” in JavaScript

#6
In short: this is a reference to a functions context. If no context is present it points to the global context or if encapsulated to undefined.

context.func() //this == context

func() // this == global

Re: “This” in JavaScript

#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 :-)

Re: “This” in JavaScript

#8
post #3

There's something to be said about the distinction between "this" and "self" as names for the self-referential instance object. While "self" emphasizes mutability and introspection, "this" evokes a more material, evidentiary abstraction. When I transitioned from PHP to Python, the change in nomenclature (albeit only a social norm in Python) was a big part of what helped me think bigger about this concept. To this poi…

I personally don't find the terminology has much significance; it wouldn't matter if it was named "the", "this", "it", "my", or "object", it all refers to "the object instance upon which this method is invoked".

Maybe it's just because I'm familiar with C where the equivalent of a "this" reference often has a different name depending on the "class", e.g. "struct Window * w" or "struct Button * b".

Re: “This” in JavaScript

#9
post #4
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...

TypeScript has these and they're marvelous. They make writing functional style code very enjoyable and not having to capture `this` yourself is alone justification for this developer to never go back to pure JavaScript.

Might you go back when ES6 is widely supported by browsers / Node? I don't mind either way, but since the parent comment mentioned that arrow functions were coming to JS, it was interesting that you specifically said 'never', rather than 'until ES6'.
Post reply on HN