I've read and heard so many developers having different opinions, and until up to now, I am not so sure whether it's bad or good.
Simple example:
{
color: 'red',
getColor: function() { return this.color; },
}
You cannot reference a field in an object from one of its methods in a generic way without using this or passing the object's ref as a parameter - which is redundant, because you already have this.
As such, this is an important part of the language. Also, let me copy and paste an answer from another thread about a programming concept:
It's a concept. A tool. Just like a hammer. You can use it in some situations, and it will work perfectly - like when you want to put a nail in the wall. However, in other situations, you should get another tool... like when you want to put a screw into the wall.
Most of the time, the problem sits in front of the computer ;)
btw, that answer gets a lot too much attention for what it reads
When I first heard the claims of it being "bad" A LOT of the "complaints" people make about it to me come across as being "I'm too stupid to use this, so nobody should." -- NEVER been a big fan of that approach. First time I heard complaints about it, that's basically what I 'thought' was being said.
But over time I've seen it abused to the point that code clarity gets utterly pitched out the window. It generally was never a problem in languages like C and Pascal... but with JavaScript I think the lack of an emphasis on the fundamentals of good practices mated to a lot of just plain blind copypasta has led to people using it before they know enough about the language to even be looking at it.
Admittedly, that describes a LOT of what people do with JavaScript. They dive for shortcuts, frameworks, and off the shelf code they don't understand before they even know enough to have an opinion as to the quality -- much less sanity -- of what they're using.
In that way, it goes back to "some people are too stupid to be allowed to have this toy" -- kind of like toy filled candies here in the colonies; what are they called? Kinder eggs? Were that such precautions were taken with ... well, let's not get political.
Of course all that said it ceases to be something that even EXISTS if you "use strict". Becomes invalid code as it just isn't allowed! In that way, using "with" in JavaScript is much akin to using <font> and <center> tags in HTML. A lot of people will see it and go "pull your cranium out of 1997's rectum!"
I shudder to think how badly people using JavaScript would get lost in Pascal or Ada... where properties and methods of an object are treated in the same scope as your locals -- equivalent to if in JS you wrote every object method as
test : function() {
use (this) {
// ALL method code here
}
}
You deal in languages like that, "with' is second nature and you just don't see it as a problem as it's just how things are done. Modern scripting languages people have this raging chodo for using the same variable names over and over again -- and that's the REAL problem. If you use verbose unique names EVERYWHERE you don't have these issues.
I'm a JS learner so I'm not sure if I can explain with authority why it's good or bad, but I can tell you based on what I've seen in other codebases:
Sometimes referring to this is inevitable, and in those cases it always makes sense.
Sometimes using this can add clarity to what you're writing, so I've seen it be used rarely and carefully and it was a bonus to legibility.
However, some people have really weird beliefs about this and use it all over the place, and have convoluted ways that they bind() and pass this around. If you see this everywhere, or things like var this = that and stuff, to me it's a code smell and it says that the developer, though they have found a way to work, probably doesn't really get how JS works.
I wish I knew more about it so I could explain what I mean better, but I'm hoping a JS developer can elaborate on the things I've seen :D
In my opinion, it is not bad. Because of dynamic nature of JavaScript, the this reference binding by context is make sense. Consider the following example to see how this binding helps us achieve inheritance in JavaScript:
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
function Employee(firstName, lastName, title) {
Person.call(this, firstName, lastName);
this.title = title;
}
Javascript is a prototype based language and "this" is a stepping stone. Creating new objects inherit from prototype methods with "this" pointing to the instance, or it is different to call a function directly or call it via a reference from an object. Many built-in javascript functions such as map, reduce, forEach, some, every ...etc will except an optional argument that allocates value to "this".
These can make your life much easier. I do understand people who tend to not like it. And if they do not want to use it, sure they can.
Aakash Mallik
S/W Engineer @ Samsung R&D Delhi
With ES6 in the game. Nope. With ES6, you would have to understand function binding to properly use this, but now with arrow functions, you will have no such issues.