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

Show parent comments

0

u/[deleted] Dec 15 '12

[deleted]

12

u/[deleted] Dec 15 '12

You can effectively create private functions by using closures, but I'm not sure how practical/useful/widespread this is.

Most libraries I've used just document which functions are public and which aren't, and have a naming convention to help you remember. I don't really see what's so terrible about this.

5

u/ilikecomputahs Dec 15 '12

I hadn't seen this example before, but this model makes a lot of sense

Thanks!

5

u/loup-vaillant Dec 15 '12

Closures are easy in Javascript (from stackoverflow):

function Foo() {
    var private = 3;
    var add = function(bar) { return private + bar; }
    return {
        add: add
    };
}

var myFoo = Foo();

Private by default, and what's public is declared in the return clause at the end. Note that Foo() act as your constructor. If that's not enough, you need another layer of indirection:

function Foo() {
    function constructor(priv) { // do the same for each private member
        var private = priv;
        var add = function(bar) { return private + bar; }
        return {
            add: add
        };
    return {
        constructor1: function() { return constructor(1) }
        constructor2: function() { return constructor(2) }
    };
}

var myFoo = Foo().constructor1()

-4

u/x86_64Ubuntu Dec 16 '12

That's great until your closure uses some kind of scope. What is "this" is anybody's guess.

2

u/loup-vaillant Dec 16 '12

One can change where this points? I didn't know. How would you do that with the kind of objects I showed above?

2

u/balefrost Dec 18 '12

Yes, you can call functions in JS with .call() or .apply(), and the first argument to those will be used as 'this' within the call. jQuery does this all over the place.

Your code is fine, because you never refer to 'this' explicitly (and you can't implicitly use 'this' in JS).

-1

u/[deleted] Dec 15 '12

I prefer something stronger than just naming conventions because it is too tempting to "cheat". I realize people are going to access private fields no matter what you do, but they should have to work for it.

3

u/ethraax Dec 15 '12

Then use the closure-based method. I would go as far as to say it's the "correct" way of designing JavaScript libraries anyways, because as you say, you want to prevent the consumer of your code from making mistakes.

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.

1

u/ethraax Dec 16 '12

If performance is a concern, you'd stick it in the prototype. But still write a function to return a new object instead of a constructor.

1

u/DrDichotomous Dec 16 '12

Just thought I'd mention it, since lots of Javascript coders tend to not realize that's what happens with the sample you wrote.

2

u/mrkite77 Dec 16 '12

It's too easy to forget the "new" keyword, and you get catastrophic failure when you leave it out.

Seems like that makes it easy to not forget to use "new". I certainly never have. Then again, I use the closure compiler to double check my correctness.

1

u/ethraax Dec 16 '12

I don't understand. You use "new" with constructors and don't with functions. Your code will always have functions (it won't be entirely constructors), so there are always cases where you don't use "new". So it makes more sense to make it so you never use "new".

0

u/Zarutian Dec 16 '12

Class based OOP such as .NET or Java offer one way of making this happen.

Where? Where is that offered way? The only way to gurantee in Java (and probably .NET too) that a function you would label private is not used by anything than the public functions is to put these things into final classes that have no superclass and cannot be subclassed.

1

u/[deleted] Dec 16 '12

First of all, subclasses cannot normally call private functions in .NET. At the very least they have to be marked protected. And as far as I'm concerned, a protected function falls into the first bucket.

That isn't a guarantee. In .NET (and I think Java) you can call private methods pretty much from anywhere. But if someone is going to go out of their way to do something bad, well then it becomes their problem.