What's wrong with JavaScript
rdallasgray.github.io
rdallasgray.github.io
Such a JS would enable them to reuse large parts (or the entirety) of the current interpreters/JITs, just as well as the next ECMAScript, but would also fix most BS and enable further performance improvements.
That is, what I propose, would be a slightly incompatible release, say: "JavascriptFixed".
As for backwards-compatibility, this would be played out in two ways. Transpilers (like Coffescript) could convert "JavascriptFixed" to Javascript, and people could use the script tag to denote the presence of "JavascriptFixed".
Given vendor support, the familiar syntax, and the performance improvements, it could take over JS in 4-5 years. No we'll have decade or more until the whole mess that's the next version becomes popular, and we'll still have the whole JS mess, just swept under the carpet.
Anyway, method_missing is not at all the same as macros. At least in Objective C that I know it's equivalent, it's very useful.
And by keeping to Javascript, only better and fixed, it would be easier to reuse and adapt huge parts of the browser ecosystem and toolchain.
Ruby maybe. Lua lacks proper unicode support. Python, I've used since 1998, but can't stand the lack of proper closures (and/or how the syntax makes them awkward) for web work.
And there's two more issues with all of them:
1) they are dog slow compared to modern JS JITs like V8 (and alternative implementations like PyPy are not as mature).
2) on the browsers, due to the sandbox and other issues, you can't use most of their libraries, that use native extensions and such, so you just get their syntax, not their batteries.
I'm thinking technologies like Skulpt or Pyjamas, that let you use Python in JS, are better. It's been a while since I investigated this situation, does anyone know of other players?
Also I would be grateful if articles about javascript would feature javascript instead of CoffeeScript.
But most importantly: It scales.
Structure is declared and not imperatively constructed. Your tools can tell what's going on without actually running the code.
I think I did miss the point with my previous comment. Maybe I was confused by the title: The article isn't "What's wrong with JavaScript", but as you said it's much more about things that will confuse Rubyists who arrive at JavaScript by way of CoffeeScript.
For an article on that topic, it does make sense to have CoffeeScript examples. But it would be much better - and so easy - to include the matching JavaScript for each of the CoffeeScript files. Not only would it help JavaScript programmers understand the article, but it would show how CoffeeScript attempts to work around these issues.
Even better would be to add matching examples of similar Ruby code. Code examples in all three languages would make for an interesting article: the Ruby way, the CoffeeScript way that makes JavaScript a bit more like Ruby, and the JavaScript way.
Read http://bonsaiden.github.io/JavaScript-Garden/#function.scope..., be enlightened
I love javascript! The main thing: offloading the computing directly to the end user.
The best cocktail...mix some JS with some CSS3, maybe throw a little HTML5 in there....yup, the only thing I find bad about JS is bad memory management, but that (like most times), comes down to code structuring.
Same deal with CoffeeScript or TypeScript. But that shouldn't stop anyone since you can just compile to JavaScript. Minifying Dart and compiling Dart to JavaScript is almost the same thing. In both cases it's a single line which calls the compiler. If you already doing one of those things, adding the other one to your build script won't hurt one bit.
You can also use the Dart executable like Node. You can use it to write command line applications or web servers.
You can also embed the VM into other applications. Like V8, the VM itself is just a library.
There will be also a Dart VM for ARM CPUs a bit later this year.
> i would chose haxe instead of Dart , since it is compatible with some parts of javascript and one can use the DOM api directly
You can also interact with JS stuff if you use Dart. DOM stuff is done via 'dart:html', which provides a very idiomatic API. For example, all list-like things are actual Lists, which is very convenient. It's similar to jQuery, I'd say.
> Dart is great in theory but has little future.
It's great in practice, too. If you don't care about IE8, that is.
Also, Google is committed to Dart. They are also already using it for a bunch of new projects.
Yes, the point of Dart is to compile to JavaScript. That's the point of any alternative language that wants to run in today's browsers.
People are still mistaken that Dart's only road to success is via all browsers adopting the VM. Dart will be just fine even if no browser, including Chrome, uses the VM. The VM in Dartium already provides for a great developer experience, and the standalone VM means it has a great server and command-line platform, and unit-testing story. Dart will be even better off if/as browsers, mobile OSes, cloud hosting providers, etc. support it.
copyObject: (inputObject) ->
copy = []
for key, val of inputObject
copy.key = val
As written, it uselessly sets a bunch of values to the key property in turn.The point stands though.
..is … irritating."
So... having one sigil to distinguish a class variable isn't irritating, but another for a variable accessed by an accessor method is. Got it.
> It’s tempting to presume that this is a ‘feature’ of prototypal inheritance
Why would this be tempting? As the author points out, there are prototype-based languages that avoid it, and you could remove prototypes from JavaScript and still encounter the issue.
> we’re not writing Java. We’re in a highly dynamic language. We want nice things. Or, we could create setters and getters using the new ES5 syntax – but that’s not available everywhere (pre-IE9, for instance), and, I would argue, is too unwieldy to apply as a rule.
You want an easy way to create accessors? OK:
function prop(name,gNs) {
function undef(v) { return typeof v == 'undefined'; }
var _name = '_' + name, get = gNs && gNs.get, set = gNs && gNs.set;
if(get && set) return function (x) {
return undef(x) ? get.call(this,this[_name]) : (this[_name] = set.call(this,x), this);
}
if(get && !set) return function (x) {
return undef(x) ? get.call(this,this[_name]) : (this[_name] = x, this);
}
if(!get && set) return function (x) {
return undef(x) ? this[_name] : (this[_name] = set.call(this,x), this);
}
return function (x) {
return undef(x) ? this[_name] : (this[_name] = x, this);
}
}
You're welcome. Use it like so: obj = {
thing: prop('thing')
}
when you want your accessors to do something other than getting/setting: obj = {
thing: prop('thing', {
get: function () { return foobarled(this._thing); },
set: function (x) { this._thing = unfoobarled(x); }
})
}
> What we’re really saying is ‘everything is a hashtable’. Is that a good thing?I'm more sympathetic to this criticism. There's times when the fact that everything can have arbitrary properties is convenient, but it does place some burden on the developer to pay attention.
Then again, I think that's a general charge to which any highly dynamic language is going to have to answer on some point or another. And if you're prone to typing `[]` when you mean `{}` (understandable if you've come from a language where they have different meanings), it's possible you should do the more conspicuous thing and use `new Array` and `new Object` instead.
> Nothing is an object
No. What the author is really talking about that by default, there aren't "methods" with class-bound scope. Just functions whose `this` scope is dynamic but does live by some pretty understandable rules.
> _missing
Missing method's sometimes convenient, and there are occasions when I miss it in JS too, but it's much rarer than I once would have thought. Use cases in languages where functions are less often let out on their own tend to get taken care of differently in languages where they do.
> I’ve yet to hear anyone convincingly argue that JavaScript’s version of OOP offers anything in addition to or distinction from more traditional versions;
"convincingly"
Convincing who?
I'm not sure you did (get it).
One is part of the language's syntax. The other is an ad-hoc convention you have to remember and check for.
>You're welcome. Use it like so: obj = { thing: prop('thing') } when you want your accessors
My eyes. The goggles, they do nothing!
>(I’ve yet to hear anyone convincingly argue that JavaScript’s version of OOP offers anything in addition to or distinction from more traditional versions; "convincingly" Convincing who?*
The author. Isn't it obvious? Also me. So make us two.
And presumably ALL the other people that write frameworks and workarounds to get traditional OOP style in Javascript.