4 ms·
I don't know much about how to architect JavaScript applications, but I am struck by how much of this stuff seems to be glued together by strings. For example,
by millstone 12y ago
I don't know much about how to architect JavaScript applications, but I am struck by how much of this stuff seems to be glued together by strings. For example, from footer.js (https://github.com/box/t3js/blob/master/examples/todo/js/modules/footer.js https://github.com/box/t3js/blob/master/examples/todo/js/mod...)
switch(name) {
case 'todoadded':
case 'todoremoved':
case 'todostatuschange':
this.updateTodoCounts();
break;
case 'statechanged':
this.updateSelectedFilterByUrl(data.url);
break;
}
Accidentally wrote 'todostatuschanged' instead of 'todostatuschange' (in keeping with the tense of all the other actions!) and you'll get a silent failure.
Or from isListCompleted() in the list module:
moduleEl.querySelectorAll('#todo-list li input[type="checkbox"]')
I'm not picking on T3, since all JavaScript frameworks seem to have this problem. There has to be a better way than that!
- glibgil 12y agoYes, it is called static analysis http://en.wikipedia.org/wiki/Static_program_analysis http://en.wikipedia.org/wiki/Static_program_analysis. It exists for JavaScript: https://github.com/sinelaw/infernu https://github.com/sinelaw/infernu https://github.com/facebook/flow https://github.com/facebook/flow
- reverius42 12y agoI don't see how static analysis helps the switch-case on strings. It might, if JS had a more expressive (static) type system. An enum type, perhaps?
- gnud 12y agoI usually do something like var Constants = { valueA: function valueA() {}, valueB: function valueB() {} };
- Mister_Snuggles 12y agoWhy do you use empty functions instead of a number or a string?
- madeofpalk 12y agoMy guess is than a function will always be a unique object, rather than if it was a number or string, there's a chance you use the same value twice.
- anewhnaccount 12y agoIn ES6 you can use Symbol.
- glibgil 12y agoIf you have static analysis then it makes sense to use types instead of strings. If you don't have static analysis then there isn't much motivation which is why you see strings everywhere in unanalyzed JavaScript.
- duaneb 12y agoNeither of those implement a type where you can enumerate the values exhaustively.
- mlvljr 12y agoBad fanboy! BAD!
- Ciantic 12y agoYou should try WebSharper http://websharper.com/ http://websharper.com/, it generates JavaScript out of F#.