Heh, why "given a chance"? I already do so!
For example: Result and Option from Rust (and get rid of undefined, null, NaN, etc.)
const Result = require('result-js');
Result.registerGlobals();
function foo() {
const e = new Error('don\'t throw, return ;)');
e.errno = 'ESOMETHING';
e.code = 42;
return Err(e);
}
const result = foo().orUnwrap('use default value, drop error');
Also, I would snatch Type declarations (also called Interfaces or Abstract Classes in other languages) from Object-Pascal, if not for my way of architecturing modules, which already ends up with something very similar.
In addition to that, I would love to have mix-able objects, so that I could compose objects instead of using prototype-inheritance... Though I use mics for that at the moment, which works pretty well!
// might work like that, when implemented into language:
const foo = { work: ()=>{ console.log('foo.work'); } };
const bar = { work: ()=>{ console.log('bar.work'); } };
const fooBar = Object.mix(foo, bar);
fooBar.work();
// Outputs:
// foo.work
// bar.work
On top of that, a more expression-oriented style (again taken from Rust)... The only hack-around is to use functions, which are called in-place. That's a little unperformant for such a simple assignment imho.
const myVar = switch (foo) {
case 'foo': break 1;
case 'bar': break 2;
default: break 3;
};
// or
let i = 0;
const someVar = while (i < 3) {
if (i % 2) {
break i;
}
i++;
};
// instead of
const otherVar = (() => {
switch (foo) {
case 'foo': return 1;
case 'bar': return 2;
default: return 3;
};
})();
Aaaand, I am missing default-immutability of variable values (as opposed to immutability of variable references, which is implemented with const), again taken from Rust (yeah, I love Rust!):
fn main() {
let i = 0;
i = 1; // ERROR! i is not mutable
let mut j = 0;
j = 1; // OK
}
Might be implemented into JS like this:
function main() {
leti i = 0;
i = 1; // ERROR! i is not mutable
consti j = { foo: 0 };
j.foo = 1; // ERROR! j is not mutable
}