3 ms·
One of the greatest things about preprocessing languages is that they usually use optimized and hard-tested features of the specific language. Let's examine Co
by drinchev 12y ago
One of the greatest things about preprocessing languages is that they usually use optimized and hard-tested features of the specific language.
Let's examine CoffeeScript ( which is one of the most popular ) :
class Foo
constructor: -> return 'bar';
foo = new Foo();
will compile to :
var Foo, foo;
Foo = (function() {
function Foo() {
return 'bar';
}
return Foo;
})();
foo = new Foo();
Which IMHO, follows the very best practices of javascript. Since JS is such a flexible language if you don't have any coding standards, your code could become hell and unmaintainable very quickly.
I would advise not to use the proposed "New-less JS", because it breaks the general coding style guides that a lot of libraries use and yes it would be possible to write it that way, but keep in mind that your code would be one step less readable from random developers.
- cromwellian 12y agoIs that really optimized/optimal? I'd be curious to see jsperf benchmarks of this method of constructor vs the straighforward approach. The JS VMs tend to kind of finnicky and optimized towards certain types of idiomatic Javascript and it's my guess that the further off the common path you stray, the more likely you are to hit performance gotchas. Your best bet is to transpile code that looks much like the benchmark suites they run. :)
- drinchev 12y agoI also agree with what you are saying. JS in terms of usage is so big that I guess developers behind JS compilers will have to optimize for "the most popular way of solving that problem". In any case, why I mentioned preprocessors is that there is a lot of history behind every feature. As of this there are a couple of github issues [1] that explain why this approach has been used. For the performance. Seems that "new" is way faster. [2] [1] : https://github.com/jashkenas/coffeescript/issues/912 https://github.com/jashkenas/coffeescript/issues/912 [2] : https://jsperf.com/obj-create-vs-new/4 https://jsperf.com/obj-create-vs-new/4
- deleted 12y ago[deleted]
- jbergens 12y agoIf you want your code to be easily readable by as many developers as possible you should probably stay away from CoffeScript.
- drinchev 12y agoThat's true, but for many reasons it helps, when you are in doubt for a way to do something. I usually use a lot of CoffeeScript's ideas. Namely class inheritance. Of course when I work in a team or on a big project I don't even consider it, because of future developers and the Bus factor.