underscore icon indicating copy to clipboard operation
underscore copied to clipboard

Call _.template() with no argument.

Open pktangyue opened this issue 13 years ago • 3 comments

Call _.template() with no argument will trigger error as the following:

Uncaught TypeError: Cannot call method 'replace' of undefined 

This error message is not friendly.

The code brought the issue in is at line 1127.

text.replace(...)

Because text is undefined.

pktangyue avatar Jan 18 '13 10:01 pktangyue

Hi @pktangyue! Because of the dynamic nature of javascript, type checking arguments would be prohibitively expensive in code size and readability. Also, I think that the error message is fairly self explanatory if not friendly, especially with a stack trace pointing to _.template and its caller. Thanks for pointing it out, but this is the intended behavior.

braddunbar avatar Jan 18 '13 14:01 braddunbar

fwiw I've allowed falsey arguments to most methods for consistency. For _.template I've just done text || (text = '');

jdalton avatar Jan 18 '13 16:01 jdalton

That seems fine to me, and in line with other methods like _.escape/_.unescape.

braddunbar avatar Jan 18 '13 16:01 braddunbar