Live data from Hacker News

Javascript Constructors and Prototypes

tobyho.com

51–55 of 55 posts

Re: Javascript Constructors and Prototypes

#51

I'm bracing for the downvote, but I'm going to say this anyway: are we all taking crazy pills? I understand that we're more or less stuck with JavaScript, but this is nuts. JS has got to be one of the least intuitive, cobbled together languages I've ever had to work with. In what other language are people still having holy wars over how to separate statements? Why the heck would a constructor be a regular function if…

> In what other language are people still having holy wars over how to separate statements?

Probably most of the languages in popular use. It's just that the fight is usually directed towards another language.

JS happens to be one of the minority where the standard allows some flexibility and debate about usage.

> mysterious constructs like prototype, new, and apply

They're well-defined, so they're not particularly mysterious if you take the time.

> Are we seriously incapable of doing better?

Really depends on how much effort you're willing to invest in learning JavaScript, instead of trying to write the code you're used to from $OTHER_OO_LANGUAGE. Everyone unwilling to do that is probably never going to be capable of doing better.

This isn't to say there's anything wrong with finding another familiar set of abstractions friendly or personally productive, but personally, I find greenfield JS is pretty fun, flexible, and capable.

Then again, I think the same thing about Perl (which everyone knows is one of the worst languages ever), so YMMV.

Re: Javascript Constructors and Prototypes

#52

I wanted to read this because I believe it's a great article, but the fact that he doesn't use semicolons pretty much turned me off. While I was looking at the JavaScript code my brain almost exploded.

I totally understand the preference for using explicit statement termination in JS. I can even understand the general preference for a non-whitespace token to terminate a statement.

But I don't understand why that would cause someone's brain to explode or otherwise make it difficult for them to pick out the author's points about the language from the example code.

Re: Javascript Constructors and Prototypes

#53
"Someone pointed out though, that you can prevent this polluting of the namespace (those are just big words for creating global variables) by using this trick:

     function Person(name){
        if (!(this instanceof Person))
            return new Person(name)
        this.name = name
     }"
This is useful for situations where you might want to be able to create anonymous objects and call a series of methods on them, which can be a nice API as any user of jQuery knows.

It's also an arguably cleaner way of achieving the constructor+apply thing he does later -- no global modification of the Function prototype.

A lot of the time, though, I'll just consider this my default "class" definition:

    function Person (args) { this.init(args); }
If you want to be warned (or want others to be warned) when Person is called w/o new, this will do it (unless someone has defined init on the global namespace, so avoid that).

This also makes it easier to hand around the method that does the actual construction, which is helpful for cases like constructor + apply:

    var p = new Person;
    p.init.apply(p,args);
(again, without global modification of the Function prototype) or for cases where you want to refer to "superclass" methods down an inheritance hierarchy.

Re: Javascript Constructors and Prototypes

#54

This article does two things that are very dangerous, and never mentions the reasons why they're dangerous. * Messing with Function's prototype is very strongly frowned upon. It's tantamount to setting global variables. At the very least, give that definition an if (!Function.new) guard to prevent redefining another implementation that another script (or the browser!) has already given. * Adding methods in the constr…

It's especially weird that the author did this after:

(a) showing awareness that polluting the global namespace is bad

(b) demonstrating a technique for being able to use a constructor with apply (the "newless" constructor he shows earlier)

but I think a lot of the article is less "here's a set of good practices" and more "let's play with some of the less-familiar corners of JS and demonstrate how flexible it is."

Re: Javascript Constructors and Prototypes

#55

I'm bracing for the downvote, but I'm going to say this anyway: are we all taking crazy pills? I understand that we're more or less stuck with JavaScript, but this is nuts. JS has got to be one of the least intuitive, cobbled together languages I've ever had to work with. In what other language are people still having holy wars over how to separate statements? Why the heck would a constructor be a regular function if…

> In what other language are people still having holy wars over how to separate statements? Probably most of the languages in popular use. It's just that the fight is usually directed towards another language. JS happens to be one of the minority where the standard allows some flexibility and debate about usage. > mysterious constructs like prototype, new, and apply They're well-defined, so they're not particularly m…

Maybe mysterious wasn't the best choice of word, but my point is that it's quite possible for something to be well-defined and yet poorly usable.

I long since gave up on trying to write the code I'm used to in other OO languages in JS. When I'm in JS Land, I put a lot of effort into using best practices. I just find it frustrating that so many of those best practices do not at all seem to adhere to any sort of principle of least surprise. And I wouldn't say that my problem is being too tightly bound to a particular OO paradigm; I've had great fun programming in functional styles and Prolog.

Post reply on HN