5 ms·
I think that part of the problem is that while JavaScript can do OO somewhat, it is not fundamentally an OO language, and if OO is the first tool that you reach
by SideburnsOfDoom 13y ago
I think that part of the problem is that while JavaScript can do OO somewhat, it is not fundamentally an OO language, and if OO is the first tool that you reach for, you are likely doing JavsScript poorly.
- cwp 13y agoJavascript is fundamentally an object-oriented language, it's just prototype-based rather than class-based.
- SideburnsOfDoom 13y agoIt seems more like a functional language to me, but YMMV: http://stackoverflow.com/a/501053/5599 http://stackoverflow.com/a/501053/5599
- kazagistar 13y agoYou seem to be under the impression that those are somehow mutually exclusive.
- SideburnsOfDoom 13y agoI'm sorry if you though so. I am not under that impression. However as mentioned elsewhere ( https://news.ycombinator.com/item?id=7500280 https://news.ycombinator.com/item?id=7500280 ) if most of what you do to organise your code involves passing functions to functions and very little of it involves creating class hierarchies or prototype chains, it's a fair assessment that the language that you are using is more functional than OO. The language that you are using may be a subset of the whole language, but with JavaScript that's given - you have to find the good parts or go mad trying. I was wrong about JavaScript as a whole, but maybe less so about JavaScript as it is successfully used.
- brianpgordon 13y agoWe do something like classes in JavaScript without touching prototypes, and it works pretty well as a natural way of organizing and encapsulating code. Private members are variables/functions defined within the constructor's closure. Public members are properties added to "this" by the constructor. Mixins can be done by calling another class's constructor on yourself. The point is that class-based OO can be trivially imposed on JavaScript objects without abandoning the native object construction mechanism like ember.js does. In fact, CoffeeScript does this in order to implement its own classes.
- jdlshore 13y agoJavaScript is a dramatically OO language. It's just not the OO you're used to. Everything* in JS other than numbers, strings, and booleans are objects. Functions are objects. See http://www.objectplayground.com http://www.objectplayground.com for details. (Temporarily down due to server problems, but hopefully back up soon.) *Not really everything. Not objects: undefined, null, number, string, boolean. Objects: object, array, regexp, function, everything else.
- AnimalMuppet 13y agoWhat definition of OO doesn't include encapsulation? And encapsulation in JS is fundamentally broken - everything is public.
- mateuszf 13y agoIt's both functional and object oriented. Also, see Scala for good combination of functional and OO.
- peterashford 13y agoYes! I most emphatically agree with this. I do a lot of OO programming in Java or C++ but I think its a horrible idiom for JS. Although I also think prototypical inheritance in Javascript is also pretty ugly :o) For me, JS seems to work best when treated like a poor man's functional language using libraries like underscore.
- SideburnsOfDoom 13y ago> For me, JS seems to work best when treated like a poor man's functional language I agree. Others have replied (rightly) that under the hood, JavaScript has a lot of objects. Functions are objects. So in that sense I am wrong: JavaScript is an OO language. However the experience of programming well in JavaScript feels more like using a functional language than using a OO language. Good JS has a lot more to do with thing such as passing functions to functions or understanding how " fn().then(fn()) " works, than it relies on class hierarchies, protypical or classical.