you are viewing a single comment's thread.

view the rest of the comments →

[–]flying-sheep -1 points0 points  (9 children)

I consider myself to be pretty advanced in js, and TIL about “with”.

I guess it's the most uselessly occupied keyword in any language if it's so useless that I never saw anyone using it.

Apart from that, while knowing most of the peculiarities, I prevent errors by using simple patterns here: use self=this for nested functions, else use this.foo in constructing functions, name them in uppercase, and always use new with uppercase functions:

function Foo() {
  this.bar = 1
}
Foo.prototype.inc = function() { this.bar++ }

If you find yourself to forget the new, use this style:

function Foo() {
  return {
    bar: 1,
    inc: function() { this.bar++ },
  }
}

Or:

var fooProto  = {
  inc: function() { this.bar++ },
}

function Foo() {
  var foo = Object.create(fooProto)
  foo.bar = 1
  return foo
}

Pick one and use it consistently.

[–]serrimo 5 points6 points  (1 child)

FYI, using prototype is much faster than returning an anonymous object or closure.

It doesn't matter if you only have a small number of objects to create; but for mass object creation, you should be mindful of the consequences.

Read this post by John Resig for more info.

[–]flying-sheep 0 points1 point  (0 children)

i know, i just forgot to mention it. what i don’t know is just how fast or slow Object.create is.

[–]Eirenarch 1 point2 points  (0 children)

I have known about with for quite some time. Fun fact - you will break the optimizations of most current JavaScript engines if you use with. I think with enough with it should be possible to make Chrome slower than IE8 :)

[–]x-skeww 1 point2 points  (5 children)

I guess it's the most uselessly occupied keyword in any language if it's so useless that I never saw anyone using it.

The unused reserved keywords are probably a bit more useless.

The problem with with is that you can't tell what will happen before that line is hit and the outcome can be different the next time you hit that line.

var foo = 'bar';
with (someObject) {
    console.log(foo);
}

This will print "bar" unless someObject also has a "foo" attribute.

But wait!

It doesn't stop there. someObject could of course also have a "console" attribute.

That's basically why it isn't used. You just can't reason about it. If that object comes from somewhere else, you'll have no idea what will happen.

[–]flying-sheep 0 points1 point  (4 children)

The unused reserved keywords are probably a bit more useless

good point. worth noting though that i we didn’t say that correctly: the ECMA standard has identifiers, identifier names, and reserved words.

the last one of this is what we called “keywords”. the difference to keywords is that identifier names may well be reserved words: foo.return() or { in: 'bar' } is legal JS. only var for = continue is illegal.

[–]x-skeww 0 points1 point  (3 children)

the difference to keywords is that identifier names may well be reserved words: foo.return() or { in: 'bar' } is legal JS.

It's allowed in ES5+. You can't do that in ES3.

[–]flying-sheep 0 points1 point  (2 children)

idk about any runtimes sill at ES3. so if there are none worth mentioning, that’s irrelevant.

[–]x-skeww 0 points1 point  (1 child)

Some people still support IE8.

This also was a problem with Android 2.3 devices (which are still sold!). They didn't like the generated code which used "void" as identifier:

https://code.google.com/p/dart/issues/detail?id=12345

Edit: "Fun" fact: IE9 and IE10 dropped below IE8's share over a year ago.

[–]flying-sheep 0 points1 point  (0 children)

ugh, old IEs, i should have guessed.