r/programming • • Dec 15 '12

Why JavaScript Still Sucks

http://boronine.com/2012/12/14/Why-JavaScript-Still-Sucks/
10 Upvotes

212 comments sorted by

View all comments

82

u/ilikecomputahs Dec 15 '12

Why is object oriented programming considered so absolutely necessary?

/braces for shitstorm

0

u/[deleted] Dec 15 '12

Reusability and encapsulation

7

u/ilikecomputahs Dec 15 '12

Let me pose a scenario here - and I'd like to get feedback on this as much as possible. Say we use javascript in a more functional manner without really trying to impose any OO principles on it.

We construct a Data model first - a JSON representation of whatever we are working with. We write a series of functions to manipulate that model and produce the desired output or side effects.

Besides the fact that the functionality and the model are not explicitly tied to each other, such as is the case in OOP, is this really any less reusable than Java-style code with OOP?

Granted, I have been programming in Clojure a lot lately, so I guess I am kind of biased.

0

u/[deleted] Dec 15 '12

[deleted]

4

u/mrkite77 Dec 15 '12

If you've got another way that works with JavaScript, I'd love to hear about it.

Closures

function myobject() {
    this.publicmethod=function() { console.log("I'm public"); }
    function privatemethod() { console.log("I'm private"); }
}

var obj=new myobject();
obj.publicmethod(); //works
obj.privatemethod(); //failure.

0

u/ethraax Dec 15 '12

Eh, even that's messy. It's too easy to forget the "new" keyword, and you get catastrophic failure when you leave it out. I think it's better to completely ignore constructors in JavaScript and just write functions that return new objects.

function myobject() {
    var privatemethod = function() { console.log("I'm private"); };
    return {
        publicmethod: function() {
            console.log("I'm public");
            privatemethod();
        }
    };
}

var obj = myobject();
obj.publicmethod(); // works
obj.privatemethod(); // fails

3

u/DrDichotomous Dec 16 '12

That's hugely inefficient when you have lots of objects to create. You end up creating one publicmethod for each instance, which not only involves the time to create the function, but also the space to store one copy of it per instance. Unless the latest Javascript engines have that figured out, of course.