我希望能够检测到我正在查看的对象是_.template的一个实例,就像我可以检查Backbone Models/Collections/Views一样。检测instanceof下划线模板
例如:
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
我采取这种代替:
typeof test == "function"
这是我的情况基本上够用了,因为我只是在我的包裹在_.template模板如果它当前是一个字符串而不是Underscore模板。
然而,我的2个问题 -
我不知道是否有一种方法来检查的instanceof _.template,目前。
如果不是,扩展模板原型链以允许此检查是否过于昂贵?除非它慢得多,否则这在Underscore中似乎是一个(次要的)错误。
如何安全/可靠是惹与['构造函数'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor)虽然?像'template.is_underscore_template'可能是一个更好的主意。 –
嗯,在这种情况下根本就不重要,而且OP似乎想要一些惯用的东西。 – nrabinowitz
它*不应该*重要,但它感觉就像用棍子戳一个马蜂窝。 –