Feed Subscribe
Exception has been thrown by the target of an invocation.


On efficiency loss with JavaScript

by ondrejsv 15. September 2011 22:34

With the hype of Windows 8 HTML5/JS apps I’m again and again rediscovering some of the pitfalls of the JavaScript language which bring issues we never have with strongly-typed languages like C#.

The first scenario is going to be quite common not only when using the new promise/then feature. (Snippet is taken from one the tutorials on the MSDN.) Look at the processPosts function that gets called when the xhr promise gets asynchronously fulfilled. There is no way the IDE would know the type (more precisely available methods and fields) of the argument and thus providing IntelliSense because the function could be called from everywhere:

image

If we write the code to run when the promise is fulfilled as an anonymous function and Visual Studio would be way smarter, it could theoretically provide IntelliSense on the result argument by parsing and processing the xhr promise but it does not so:

image

The second pitfall comes from the fact that any code could be run in a context of any HTML file. When you reference a .js file from an HTML page, some of the global variables get alive by getting references of the screen elements with variables of the same id. If you make a typo in the name of a variable, you would not find it out until you run the page. Of course, there is no IntelliSense either because the variable could be of any dynamic type at run-time. This pitfall is really a feature of all dynamic languages but for a programmer used to program user interfaces in ASP.NET and Silverlight which have concepts of code-behind coded in strongly-typed C# it represents a loss in efficiency, indeed.

image

image

Tags: