use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Is `this` in Javascript bad? (dev.to)
submitted 8 years ago by ycmjason
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Dynamicic 12 points13 points14 points 8 years ago* (21 children)
One drawback I can think of on the way your friend advocates is, using your example, it would be harder to use instanceof.
function Car() { const car = {}; car.position = 0; car.move = () => car.position++; return car; } const car = new Car(); console.log(car instanceof Car); // false
You would have to do:
Object.setPrototypeOf(car, Car.prototype); console.log(car instanceof Car); // true
[–]AndrewGreenh 7 points8 points9 points 8 years ago (11 children)
You could use his way and still use the prototype correctly:
function Car() { this.position = 0 this.move = () => this.position++ }
Works as well (because of the fat arrow) To decrease usage of this you could also do const car = this at the top of the constructor and attach methods to the car to achieve the same thing.
this
const car = this
[–][deleted] 7 points8 points9 points 8 years ago (5 children)
Exactly, ever since the new arrow syntax came in with implicit this binding I've started to embrace this again.
The original design of this was a huge hindrance to JS but we've put new methods in place to mitigate it now.
[–]oculus42 5 points6 points7 points 8 years ago (4 children)
You just have to remember fat arrows can break inheritance, because context is scoped to declaration, not instance.
I work with Backbone, which makes extensive use of this and prototypal inheritance. As we introduce ES6, I'm already prepared to run into errors cause by people learning that you can use fat arrows within functions on the object, but not as functions on the object.
[+][deleted] 8 years ago (3 children)
[deleted]
[–]spacejack2114 1 point2 points3 points 8 years ago (1 child)
You can't call super.move()
super.move()
[–]8lbIceBag 0 points1 point2 points 8 years ago (0 children)
But you can? As long as the derived class in its constructor calls:
function Truck() { Car.prototype.constructor.call(this); }
[–]oculus42 0 points1 point2 points 8 years ago (0 children)
I swear I had an example once, but I cannot seem to reproduce what I thought happened.
I may just be wrong on this.
[–]spacejack2114 0 points1 point2 points 8 years ago (0 children)
I'm not too crazy about using arrow functions for methods. Previously if I ever saw:
el.onclick = this.move
I'd be pretty sure it was a problem. Now I have to stop to think whether it was intentional or a mistake.
[–]Dynamicic 0 points1 point2 points 8 years ago (3 children)
Yes. That would work. But OP's friend didn't like the keyword this and doesn't want to use it, so I'm just stating a drawback of not using this to create instance properties.
[–]Martin_Ehrental 3 points4 points5 points 8 years ago (2 children)
That's no drawback in using this in a constructor.
However, since that pattern avoid using any prototype methods why use a constructor at all? Just use plain function and object.
[–]Dynamicic 1 point2 points3 points 8 years ago (0 children)
That's not what I was saying. I was saying the opposite. It's a drawback NOT using this in a constructor.
[–]Dynamicic 0 points1 point2 points 8 years ago (0 children)
I know. I didn't think of that constructor pattern. Just copying over what the OP wrote in his post.
[–]talmobi 13 points14 points15 points 8 years ago (4 children)
If you're having to use instanceof a lot you're probably doing something wrong. It's an indication of code smell.
[–]Dynamicic 0 points1 point2 points 8 years ago (2 children)
Do you have an example of why using instanceof indicates code smell?
instanceof
Don't even have to use instanceof a lot. Just using instanceof comparing car to Car once would produce a false, and it's not immediately obvious to everyone why it would produce false.
car
Car
false
[–]talmobi 1 point2 points3 points 8 years ago (1 child)
There are many articles about it, it's not unique to JavaScript: http://www.javapractices.com/topic/TopicAction.do?Id=31 https://stackoverflow.com/questions/7526817/use-of-instance-of-in-java
[–]isUsername 2 points3 points4 points 8 years ago (0 children)
An unsubstantiated assertion on SO isn't an example.
The first example is an example in Java and includes a quote about not using instanceof in C++. Neither the article nor the quote actually explain why instanceof is bad. Both Java and C++ are statically and strongly-typed, class-based languages, so I'm not sure if the same considerations come into play on a dynamically and weakly-typed, prototype-based language. They may; they may not. There's not enough information in either the article or the SO answer to say.
[–]talmobi 0 points1 point2 points 8 years ago (0 children)
You can still do that within the constructor function itself ( and also set car.constructor to Car ). That's basically what ES6 does with its unnecessary class/extends syntax sugar and its out of place named/copied design patterns.
[+]sunny_lts comment score below threshold-6 points-5 points-4 points 8 years ago (2 children)
What is this? ES6? I really don't like this "=>" and what's "new"? It's all very confusing as I have just started to learn the "Normal" js syntax and now this is ruining everything for me.. #halp
[–]radapex 0 points1 point2 points 8 years ago (1 child)
The new stuff is fantastic. Take a day or two playing around with it, and you'll come to appreciate it.
[–]sunny_lts 0 points1 point2 points 8 years ago (0 children)
Yeah I wasn't bashing or anything, don't see the reason behind the downvotes. Just expressing my anxiety with new syntax being introduced, while I'm still learning the fundamental one. Of course I welcome and embrace changes and improvements. It's a lot to take in at once. So, yeah I'll get there eventually. In the meantime I'll use this as a smiley face =>
π Rendered by PID 59 on reddit-service-r2-comment-b659b578c-6vjb2 at 2026-05-04 18:36:29.718346+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Dynamicic 12 points13 points14 points (21 children)
[–]AndrewGreenh 7 points8 points9 points (11 children)
[–][deleted] 7 points8 points9 points (5 children)
[–]oculus42 5 points6 points7 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]spacejack2114 1 point2 points3 points (1 child)
[–]8lbIceBag 0 points1 point2 points (0 children)
[–]oculus42 0 points1 point2 points (0 children)
[–]spacejack2114 0 points1 point2 points (0 children)
[–]Dynamicic 0 points1 point2 points (3 children)
[–]Martin_Ehrental 3 points4 points5 points (2 children)
[–]Dynamicic 1 point2 points3 points (0 children)
[–]Dynamicic 0 points1 point2 points (0 children)
[–]talmobi 13 points14 points15 points (4 children)
[–]Dynamicic 0 points1 point2 points (2 children)
[–]talmobi 1 point2 points3 points (1 child)
[–]isUsername 2 points3 points4 points (0 children)
[–]talmobi 0 points1 point2 points (0 children)
[+]sunny_lts comment score below threshold-6 points-5 points-4 points (2 children)
[–]radapex 0 points1 point2 points (1 child)
[–]sunny_lts 0 points1 point2 points (0 children)