Friday, 23 August 2013

Detect instanceof Underscore template

Detect instanceof Underscore template References I would like to be able to detect if an object I\'m looking at is an instance of _.template, the same way I can check for Backbone Models/Collections/Views. for example: var newView = new Backbone.View(); newView instanceof Backbone.View //true //How I usually use template var test = _.template(\"test\"); test instanceof _.template //false //When I would actually expect a successful instanceof check var test2 = new _.template(\"test\"); test2 instanceof _.template //false I\'m resorting to this instead: typeof test == \"function\" This is basically good enough for my circumstances, since I\'m just wrapping my template in _.template if it\'s currently a string instead of an Underscore template. However, my 2 questions - I\'m wondering if there\'s a way to check instanceof _.template, currently. If not, is it prohibitively expensive to extend the template Prototype chain to allow this check? Unless it\'s much slower, this seems like a (minor) fault in Underscore.

No comments:

Post a Comment