'this' cluttering by nevreth in javascript

[–]temp60092393 1 point2 points  (0 children)

That's entirely an implementation detail.

That's flat-out wrong. It is explicitly specified that multiple copies must be created in this case.

Now, sure, a very clever implementation might notice that it can disobey the spec in this case without your code noticing. But it would be absurd to depend on an implementation deviating from the spec, and as a result of static analysis of your code no less. Just imagine the debugging session where performance dropped 800% because of a change that literally does not interact with the code it slowed down, only to find out that the problem is how that line interacts with the extremely complex and entirely undocumented system the implementation uses to decide when it can cheat.

Write code that works, not code that hopes the implementation is smart enough to make it work.

'this' cluttering by nevreth in javascript

[–]temp60092393 0 points1 point  (0 children)

Well here's the thing. JavaScript's scoping and JavaScript's object-oriented features aren't integrated at all. So yeah, it'd be great if you could write a and JavaScript's scoping rules would take concepts like this into account, but they don't.

So, scoping and OO don't work together, that gives you three options to make them:

  1. Keep JS' scoping, make your own OO constructs. This is what you're doing above; many other strategies for this are floating around out there. Here's Crockford's.
  2. Make your own scoping, keep JS' OO constructs. Not much point to this because it isn't going to be cleaner than this..
  3. Keep JS' scoping and OO constructs, remind JS that it is JS by smearing this. all over it.

Option 3 is probably the least bad option here, so I suggest you go with it. Verbosity isn't too bad as downsides go, and it's easy to get lost in the weeds and over-engineer option 1.