By the way, `typeof X === 'undefined'` doesn't need to be used for variable names that exist such as arguments, you can use X == undefined without opportunity for failure.
Technically, all edge cases considered, `typeof` is the only fool-proof check, since you can't override the string constant `'undefined'` but the `undefined` identifier is fair game.
Here is one such edge case:
(function () {
var undefined = true;
return (function foo(x) {
return x === undefined;
}());
}()); // false
Language pedantry aside, browsers these days will freeze `window.undefined` so you'd have to shoot yourself in your own scope ;)
Looks like the readme example doesn't work. I didn't compile the djs sayHello function to verify it matches the given javascript, but running sayHello("nyc") spits out
Hello to undefined, nyc
You didn't tell me your age
1. Instead of implementing it in vanilla JavaScript, consider creating a Sweet.js macros (or series of macros), so it can be used with other flavors of JS as well (e.g. JSX, etc)
2. On the async functions – what happens if you put an async function inside a conditional? This is effectively solved by ES6's yield + co or an equivalent.
3. A nitpick, but, is there a way to create static functions on classes? E.g. Car.x instead of Car.prototype.x?
17 comments
[ 3.0 ms ] story [ 49.5 ms ] threadNevertheless, good luck with the project and everything.
Here is one such edge case:
Language pedantry aside, browsers these days will freeze `window.undefined` so you'd have to shoot yourself in your own scope ;)If you give a third, the second will be your age and the third your city.
Example:
2. On the async functions – what happens if you put an async function inside a conditional? This is effectively solved by ES6's yield + co or an equivalent.
3. A nitpick, but, is there a way to create static functions on classes? E.g. Car.x instead of Car.prototype.x?
2. I'm not sure if I understand your correctly, but it should work if you wrap it in (). I actually have that very same example in 'broken.js' [0]
3. Sure. As you can write normal JavaScript, just use:
The ':' operator is supposed to be a shortcut for X.prototype.Y[0]: https://github.com/binlain/designtojs/blob/master/test/broke...
I'm asking because I built something similar (though it only worked with promises, not general async functions), and I'm curious to compare approaches :-) - mine is a sweet.js macro: http://pastie.org/9410315 (uses https://github.com/petkaantonov/bluebird and https://github.com/BranchMetrics/promise-accum). The basic syntax would be:
(again, beautified because DesignToJS crams everything in one line to preserve the line numbers of your actual code):
If you omit the function body, it will only expand to the end of the current logical 'context' (or whatever it's called)I'd suggest writing it like this in DesignToJS:
Result looks like this: Edit: I just submitted a patch that avoids unnecessary binding of 'this' if 'this' is not actually used.There's a test case in 'test/namedThings.(d)js'