After posting a script recently I received a bug report, more specifically my code was working yet it was not well to debug since I forgot to use a 'var' keyword in a loop. We had a talk about the magic of Javascript (or JScript which is used by DOpus) which reminded me of that talk on youtube that I wanted to share with you, just to enjoy when you are annoyed by Jscript or you simply want to see black magic tricks in js. Enjoy
Assigning to an undefined variable, automatically creates a global variable, just to clear that. o)
Regarding the magic, yes.. every JS developer should have watched videos like that. Sometimes you really scratch your head with JS, but you get used to some of the magic and even start to incorporate it into your code, like simply adding an empty String "" to objects if you want to cast to String or get the default property. Also simply adding +0 if you want to make sure, you are dealing with a number, is something I use a lot. Adding the "10" as second paramater to parseInt( .., 10) also becomes second nature quite quickly.
If I remember PHP has even weirder behaviour, but for some reason, you can still create working applications with these languages, they work most of the time at least and hopefully you don't end up with too many bugs, because you did not handle some of these edge cases. o)
It also seems like unexpected behaviour is not going to die, in scripting languages at least.
If you look at more modern things like Powershell, there are a lot of issues in there as well, returning an array from a function, which only contains one element will throw the array away and unwrap the single value. I wonder who comes up with ideas like that. This specific behaviour is there to make Powershell more handy on the command line, but seriously.. if the datatype of your variable changes back and forth, how are you supposed to work with that? o) The solution to this is to always wrap an array returned from somewhere into another array, just horrible from my point of view, but little we can do about it. o)
Thanks! o)
JS is a much much more potent and interesting language than some people give credit for. But since the entry threshold was so low, some bad practices got very common.
Magic word for me is TypeScript, with most of the good magic bits of JS, without most of the ugly bits of JS. Douglas Crockford has been talking about those bits for years now.
Indeed, TypeScript is the ideal scripting language, it's better than even the Python for larger projects.
I've seen your topic about developing Opus scripts with TypeScript and it would be really beautiful if Opus would support TypeScript directly one day.
(Also perhaps Microsoft will one day deprecate the legacy JScript, similar to how they announced for VBScript).