login - help - about
?
header
<< Back to News

Deprecating javascript functions
With my recent experience with object oriented javascript during the autocomplete refactor, I've noticed some older javascript library functions which should be in prototypes. To make the changes without breaking existing code, I've kept the method declarations and had them call the new prototyped functions. I now just needed a way to nicely deprecate the functions. Here it is, using firebug and stack traces:
var _shownDeps=new Array(); function deprecated(because) { if (typeof console != 'undefined' && console.warn) { var sig=''; var from=''; try { i.dont.exist+=0; // cause an exception } catch(e) { var lines=e.stack.split('\n'); sig=lines[1].split(')')[0]+')'; from=lines[2].split(')')[1].substring(1); } if (!_shownDeps[from]) { console.warn('DEPRECATED: '+sig+'\ncalled from: '+from+'\n'+because); _shownDeps[from]='true'; } } }
A simple call to deprecated('reason for deprecation') causes a nice display in firebug log. Due to the shown tracking, deprecation messages are only shown once for each calling line. Here's a result from the firebug console log:
Having the call location shown (and only shown once) is very useful for quickly fixing the deprecated call. Note that this only works for firefox due to the firebug console and stack mechanism. It will require rework for other browsers. Thanks to Eric Wendelin for the stack code.

... and, yes, the deprecated function should be deprecated as it's not been prototyped ;)

- 10:24 PM, 29 Mar 2011