JavaScript Minify Tools and the objectStore delete Function

April 24, 2012

no comments

JavaScript Minify Tools and the objectStore delete Function

One of the good practices when deploying web sites or applications is to minify your JavaScript/Css files. Minifying the files can help to improve a little the performance of your site or application since it will minimize the impact on the network.

In most of my web projects, I add a MSBuild task to minify the JavaScript/Css files using one of the following tools: Microsoft Ajax Minifier or YUICompressor for .Net. Both of them are good enough for my needs and very easy to set in Visual Studio environment.

Lately, I needed to minify JavaScript code that includes the use of IndexedDB. I have added the minifying tool as always and minification failed. The reason for that is the use of objectStore delete function. delete is a reserved word in JavaScript and it is main purpose is to remove properties from JavaScript objects. In most of the IDEs you will see that the IDE will mark it as a reserved word and sometimes nag you with warnings such as “Expected identifier” when you use IndexedDB. Trying to minify JavaScript code with a call to the delete function such as:

var request = store.delete(key);

will fail in most of the minifying tools. In order to succeed in the minification process, I needed to “uglify” my code and to turn the previous JavaScript statement to the following valid JavaScript statement:

var request = store["delete"](key);

You can use this form of code since in JavaScript you can call an object property/function in both of the ways – using the dot notation or using square brackets.

This is the case were using a reserved word can bite you. I would have expected one of two things:

  • W3C would be aware of that and maybe call the objectStore’s function remove in the specifications. This won’t break very necessary tools.
  • The tool writers will be aware of this problem and fix it in the minification process.

I hope you won’t be forced to use the “uglify” alternative in the future but who knows.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>