4 ms·
It has nothing to do with OOP. Off the top of my head, I cannot think of another OOP language where "this" (or "self") works the way it does in Javascript. Ther
by zeroname 8y ago
It has nothing to do with OOP. Off the top of my head, I cannot think of another OOP language where "this" (or "self") works the way it does in Javascript. There are pitfalls associated with it and I can't think of any benefit that would outweigh that.
- anothergoogler 8y agoGood luck doing OOP in JS without `this` with minimal contortions. And to be clear, there is no `self` in JS.
- benbristow 8y agovar self = this;
- anothergoogler 8y agoYes, I know. That doesn't make an assertion about the "pitfalls" of `self` less muddy. It's literally a variable. It's a code smell too, but that's another conversation. Are we calling out variables in JS now? Maybe we should just hold ourselves to bootcamp standards, copy and paste StackOverflow or something like that. Programming is hard, etc.
- chriswarbo 8y agoI find this comment amusing, considering that Javascript's OOP ideas are descended from Self ( https://en.wikipedia.org/wiki/Self_(programming_language) https://en.wikipedia.org/wiki/Self_(programming_language) )
- weberc2 8y agoWhy would you want to do OOP in JS?
- tyingq 8y agoI like the "Python/Perl/Probably others" approach of the self object just being passed as the first element to methods. No magic names.
- megous 8y agoThat would be horrible in JS. All functions would have to have an explicit frist parameter that would be often unused, because you don't know how someone will use the function. function x(obj, param1) {} class X {} X.prototype.x = x; o = new X(); o.x(123);
- tyingq 8y agoWell, yes, you would have to only apply it to methods, as Python and Perl do. I'm not suggesting it's practical to graft all that into JS at this point.