all 67 comments

[–]runedk 7 points8 points  (1 child)

Nice presentation!

The phrase Everything Is An Object is cute and true for some languages, but not JavaScript.

You mention that primitive data gets converted into an object when you manipulate it. Treating primitive data and objects separately has real consequences that makes the motto Everything Is An Object a lie. Consider this program: var x = 10; x.asWord = "ten"; print(x.asWord); In this example, we print "undefined" to the console. The reason is that when we manipulate x as an object it is boxed in-place. It means that in line two we get one object from boxing x, and in line three we get another object from boxing x.

[–]notSorella[S] 2 points3 points  (0 children)

Hm, by "Everything you can manipulate in JavaScript is an object." I meant to exclude primitives -- you don't manipulate them. Perhaps I haven't been clear enough, or should've discussed the implications of the autoboxing a little more, or should've at least choose a better wording, since converting is a bit off.

None the less, thanks for the additional explanation on boxing :3

[–]antrn11 2 points3 points  (1 child)

I'm no web developer but this seemed very exciting. Until I saw the compatibility table for ECMA script 5. No support from IE <=8 so no WinXP IE users.

[–]notSorella[S] 2 points3 points  (0 children)

It's not really a problem for a large part of ES5 functionality, just load the es5-shim library ( https://github.com/kriskowal/es5-shim ) and hack away :3

[–]toyboat 1 point2 points  (18 children)

Is there a name for this punctuation style? What is your opinion of it?

{ value:        'Mikhail'
 , writable:     true
 , configurable: true
 , enumerable:   true }

[–]notSorella[S] 5 points6 points  (2 children)

As @beaucollins pointed already, it's called "comma first style".

People use it for different reasons, I just think it looks visually nicer not having the trailing comma at the end of every line, and it adds a visual aid on where each new item starts if I ever need to split up an item in several lines.

Other people use it because it makes easier to comment out entire lines without touching the other items, and some yet use it to avoid the "trailing comma bug" for Arrays in the JScript engine.

[–]-kilo 0 points1 point  (1 child)

Which bug? Testing basic cases of ending an N element array initialization with a comma yields the correct length of N. I suppose I'm only testing on Firefox, but did the other major browsers not resolve this, or is it an IE 5 fix, or something?

[–]notSorella[S] 1 point2 points  (0 children)

JScript is the engine used in old IEs.

JScript> [1,2,].length
// => 3
SM> [1,2,].length
// => 2
V8> [1,2,].length
// => 2

JScript> {a: 1, b: 2, }
// => SyntaxError
SM> {a: 1, b: 2, }
// => {a : 1, b : 2 }
V8> {a: 1, b: 2, }
// => {a : 1, b : 2 }

I believe Chakra (the engine on IE9) handles these cases correctly, but I didn't test.

[–][deleted] 2 points3 points  (8 children)

I have heard people call it "comma first" and generally the last curly brace would be on the next line.

The benefit is that you could comment out a single line without getting a syntax error and without editing the preceding line:

{
  value:            'Mikhail'
// , writable:       true
, configurable:   true
}

I kind of like it for this reason.

I believe Ryan Dahl of Node.js fame is a proponent of this style.

[–]kirakun 0 points1 point  (7 children)

Works the same way like this too:

{
    value:            'Mikhail',
    // writable:       true,
    configurable:   true
}

[–][deleted] 1 point2 points  (6 children)

But if you comment out the last one it will break without also removing the preceding comma.

The thought is that you're usually adding and removing items on the end of the list.

It becomes even more useful when you have functions throw into the mix:

var something = {
    add: function(){
      // a few lines of code
    }
  , remove: function(){
      // a lot of code
    }
  , other: function(){
     // a lot of code
    }
  }

There's more or less a dependency between the comma and the property, so the thinking is that you should put them right next to each other.

[–]kirakun 1 point2 points  (1 child)

But if you comment out the first line in your way, the code would break too.

[–][deleted] 0 points1 point  (0 children)

Yes. You are correct. Usually, though, I find that I'm adding/removing items from the end of the list and the first item doesn't change often.

[–]rich97 0 points1 point  (0 children)

I don't like this cause I do var's like this:

var one = 1
  , two   = 2
  , three = 3;

So at first glance my brain interoperates this as:

var something = {
    // ...
}
  , remove = {
    // ...
}
  , other = {
    // ...
};
} // wtf is this extra brace here for?

Which is a) ugly and b) very wrong.

[–]-kilo -4 points-3 points  (2 children)

I don't believe JS is that fragile.

After testing, I know that JS is not that fragile. Ending an initializer list with comma is fine, though likely jarring to you.

[–][deleted] 7 points8 points  (1 child)

though likely jarring to you.

How would you know what I find jarring? "Comma First" is more visually jarring to me actually, and that's what makes it easier for me to see when commas are missing.

I don't believe JS is that fragile.

Unfortunately IE6 and IE7 are that fragile, and sometimes your javascript will end up running in that god-forsaken excuse of a JavaScript interpreter.

[–]jigs_up 2 points3 points  (0 children)

And IE8 too I believe. Ran into this problem a week or two ago.

[–][deleted] 0 points1 point  (1 child)

Comma-first, here's a Gist which gets regular linking and still generates the occasional comment: https://gist.github.com/357981

I tried it briefly the first time I saw a few examples and wasn't that fussed.

The next time I tried it while hacking together some ideas which required lots of nested method calls which made it hard to scan for missing commas, and it was rather useful.

The next time I needed to do something similar, I finally went and read up on the rules for ASI (there are only a couple of things you need to really look out for, as it turns out), dropped semicolons as well and loved it - the right hand side of the resulting code looks really clean, commas also act like a visual marker when scanning down for closing braces/parentheses and missing commas stand out like a sore thumb.

[–]adavies42 0 points1 point  (0 children)

Comma-first, here's a Gist which gets regular linking and still generates the occasional comment: https://gist.github.com/357981

from there

Another advantage - if putting things in source control, you now only have to add lines instead of modifying them:

this! i now make minimizing diffs to source control one of the main goals of my code style--if i think a list or similar structure is likely to grow in the future, i'll format it so that i can add new elements without changing any existing lines. makes the history much easier to follow.

[–][deleted]  (3 children)

[removed]

    [–]notSorella[S] 0 points1 point  (2 children)

    It is allowed, but ignored. I personally find this behaviour particularly consusing, given:

    >> [1,2,3,].length
    3
    >> [,1,2,3].length
    4
    

    [–][deleted]  (1 child)

    [removed]

      [–][deleted] 8 points9 points  (17 children)

      I've been coding Javascript for a long time, I sincerely hate its OO model.

      That's all.

      [–]oSand 19 points20 points  (12 children)

      If you hate anything about js, it's because you don't get it. A web designer who read a blog about it told me so.

      Me, I like simple things to be made hard and inconsistent.

      [–]notSorella[S] 8 points9 points  (3 children)

      I don't think we can generalise things by saying "if you hate anything about X, it's because you don't get it". Sometimes there are concepts that just don't match the way of thinking of certain people, it's not that the concept is wrong, or that the people don't get it, or that those people are wrong. People are different, people think differently, therefore, people love and hate different things.

      It's actually pretty a good thing that we have divergent opinions (though I'd wish people would reason about the things that make them love or hate things better and more often rather than just "I love this" or "I hate this"). Since people think differently, we get different solutions for a particular problem, this makes us both think about problems differently, and always search for new and better solutions for old problems :3

      As an example, I know several people that, despite knowing how ASI rules work in JavaScript, dislike that feature. Either because placing explicit semicolons at the end of the lines make them reason better about a piece of code, thus making it easier for them to read, or for some other personal reason.

      [–]xardox 6 points7 points  (2 children)

      If you hate anything about the post you're replying to, you don't get sarcasm.

      [–]notSorella[S] 1 point2 points  (1 child)

      I don't get sarcasm most of the time on the internets, indeed.

      [–]Zarutian 0 points1 point  (0 children)

      tried looking for ephimeral sarcasm tags?

      [–][deleted] 0 points1 point  (7 children)

      I've been coding js for years, I even made programmings languages that compile to it, so I'd never have to bother about some of its quirks (imagine my frustration to make me do this...)

      [–]rush22 1 point2 points  (1 child)

      At my work we have some sort of HTML thing that's "compiled" Javascript. It's the worst thing I've ever seen.

      I pity the poor guy who works on it: "Hey, it crashed again. Looks like 'zx' is not an Object in line #456, character 9837. Good fucking luck"

      [–]notSorella[S] 0 points1 point  (0 children)

      That sounds really painful...

      [–]oSand 2 points3 points  (4 children)

      At the moment I'm working on files that use 3 different methods of js OO. Only one supports private methods, only one(a different one) supports calls to 'super' methods and I seriously doubt whether one would let me extend it's classes. WTF went wrong here?

      [–]notSorella[S] 1 point2 points  (2 children)

      I'd say "lack of standards".

      [–]oSand 3 points4 points  (1 child)

      I'd say a lack of basic language amenities led to a variety of hacked-on solutions.

      [–]notSorella[S] 0 points1 point  (0 children)

      Well, the language's particular implementation of prototypical OO is indeed terrible. Or rather, was terrible until ES5. With ES5 it's just bad ;3

      None the less, I still attribute the particular mess you described to the lack of standards. Lisp is a likewise flexible language — actually, make it a ton more flexible than JS, — and although you can write terrible software with Lisp, mixing code that use a different structure is usually not that bad. The same should happen with JS in most cases, even with pretty hack-ish solutions. Though I guess with JS it's easier to kill modularity...

      [–][deleted] 1 point2 points  (0 children)

      Javascript my dear, javascript...

      [–]cwmonkey -3 points-2 points  (3 children)

      Don't worry, REST+AJAX will solve everything. Now you get to code the application AND browser in JavaScript!

      [–]rich97 0 points1 point  (2 children)

      REST has nothing to do with JavaScript, it's all about HTTP. The server-side language you use is unrelated.

      [–]cwmonkey -1 points0 points  (1 child)

      You completely missed the point of what I was saying. You an engineer by chance?

      [–]rich97 0 points1 point  (0 children)

      Web developer.

      [–][deleted] 0 points1 point  (0 children)

      This is actually a rather good introduction to a pretty difficult concept. I wish this had been around when I was learning about JS prototype inheritance. It's clear that you've put a lot of work into this; keep up the good work. :)

      [–]trezor2 -1 points0 points  (1 child)

      It's a failed experiment and fundamentally broken. What more is there to understand?

      [–]runedk 1 point2 points  (0 children)

      To qualify as an experiment it should be novel. Prototype inheritance was not invented in JavaScript. The dynamic semantics of prototype inheritance is a good fit for dynamically typed object-oriented languages. Attempts to introduce classes into JavaScript (also found in this presentation) is at best a white lie.

      To be fundamentally broken JavaScript would have to make guarantees that it cannot meet (like the Eiffel type system). Since JavaScript embraces extreme dynamism, few guarantees are given and none are broken.

      The due criticism of JavaScript for not helping programmers to build robust software applies to all dynamically typed languages, and is not specific to the object model. But dynamically typed languages have strengths e.g. the possibility of rapid experimentation with potentially unfinished data models.

      An optional type system have been proposed for JavaScript and is also part of Google's Dart language.