4 ms·
I don't actually write object oriented (class-based or prototypal) code in Javascript, I write functional code instead and it seems to work well. Am I missing s
by ntoshev 16y ago
I don't actually write object oriented (class-based or prototypal) code in Javascript, I write functional code instead and it seems to work well. Am I missing something? Any examples of "actual applications that are best modeled with prototype chains"?
- Swizec 16y agoIn general I've found that Javascript suddenly becomes a very awesome and even understandable language once you start using it as functional.
- calebmpeterson 16y agoThis has been my general experience too. I've found myself taking Crockford's description of "Scheme in C's clothing" to heart. I treat object's more as first-class maps/associative arrays - much in the same way that maps are first class in Clojure - than I do as proper OOP objects. Functional JavaScript is exciting and maleable to me, which is a breath of fresh air after (still) waiting so long for proper closures and anon functions in Java.
- olavk 16y agoThis is a semantic quibble, but I disagree that objects in JavaScript are any less proper OO just because they are implemented as associative arrays. JavaScript is a fundamentally OO language in a way Scheme is not - for example all built-in types in JavaScript are objects with methods. I think OO is getting a bad name due to people annoyed with Java, and functional is hip these days. But JavaScript is still an OO language.
- chc 16y agoI think you're a little bit eager to defend JavaScript's OO credentials here. Nobody said anything like "objects in JavaScript aren't proper OO" or "JavaScript is not an OO language." Caleb said that he prefers to use a functional style of programming in JavaScript and tends to treat objects as simple maps.
- calebmpeterson 16y agoGlad to see that my explanation wasn't hopelessly incomprehensible. Now if only JS supported immutability... I think I'll go learn some more Clojure. :)
- olavk 16y agoIn ECMAScript 5 there is support for immutability with Object.seal(), Object.freeze() and Object.preventExtensions()
- olavk 16y agoYou are right. Apparently I cant read.
- calebmpeterson 16y agono problem at all-happens to me regularly
- calebmpeterson 16y ago> This is a semantic quibble, but I disagree that objects in JavaScript are any less proper OO just because they are implemented as associative arrays. JavaScript is a fundamentally OO language in a way Scheme is not - for example all built-in types in JavaScript are objects with methods. Not even a semantic quibble - more like me poorly expressing my typical use of objects in JavaScript. I agree - objects in JS are full objects. I just tend to limit my use of them to the associative array realm... > I think OO is getting a bad name due to people annoyed with Java, and functional is hip these days. Agreed. I've written ~250K lines of Java (real-time, highly parallel and concurrent systems with hardware accelerated UI) in the past 3 years and love many things about the language and (especially) the JVM. FP is certainly in vogue right now - and for mostly good reasons. Hopefully as the pendulum swings we'll end up with a healthy middle-of-the-road approach which incorporates OO and FP. After all, closures are a poor man's object and objects are a poor man's closure. > But JavaScript is still an OO language. Most certainly! I was just highlighting the ability to (mis)use it as a functional language. Though as a functional language its lack of immutability is a double edged sword...
- perlgeek 16y agoMaybe that's what should be filled in where the author says "I’m really wondering what’s next on this list". But I don't think it's the logical next step in the evolution described - it's rather what happens if you learn Lisp or Haskell or so and then revisit your javascript practises.
- olavk 16y agoMaybe your own code is functional, but the DOM and browser API you interact with is a prototype-based object model. A JavaScript application is basically event handlers attached to the DOM/browser framework, and you produce output by changing properties on these objects. So I don't think you can escape the OO paradigm. (In most cases you don't need to know if the DOM is class based or prototype-based though. It only matters if you use advances features like modifying the prototype of an element.) Even if you work outside the browser, you still have to work with the basic types like arrays in a OO manner.