Javascript 在下划线模板中使用整型变量作为键

Javascript 在下划线模板中使用整型变量作为键,javascript,underscore.js,underscore.js-templating,Javascript,Underscore.js,Underscore.js Templating,根据下划线,它允许您执行以下字符串模板/插值操作: var compiled = _.template("hello: <%= name %>"); compiled({name: 'moe'}); // => "hello: moe" var compiled=ux0.template(“hello:”); 已编译({name:'moe'}); //=>“你好:莫” 我不知道是否有任何方法可以使用整数作为键,而不是使用“name”这样的变量符号?例如: var compi

根据下划线,它允许您执行以下字符串模板/插值操作:

var compiled = _.template("hello: <%= name %>");
compiled({name: 'moe'});
// => "hello: moe"
var compiled=ux0.template(“hello:”);
已编译({name:'moe'});
//=>“你好:莫”
我不知道是否有任何方法可以使用整数作为键,而不是使用“name”这样的变量符号?例如:

var compiled = _.template("hello: <%= 1 %>");
compiled({"1": 'moe'});
// => "hello: moe"
var compiled=ux0.template(“hello:”);
已编译({“1”:“moe”});
//=>“你好:莫”

我尝试了一下,但是下划线JS模板将其作为文本而不是变量进行计算,如果提供的变量中包含这样的整数键,是否有任何方法可以使用下划线进行模板化?谢谢

不,不能使用整数,必须使用有效变量名称,这意味着它可以有整数,但不能仅是整数

有效的方法是:

compiled = _.template("hello: <%= a1 %>");
console.log(compiled({a1: 'moe'}));

compiled = _.template("hello: <%= _1 %>");
console.log(compiled({_1: 'moe'}));


compiled = _.template("hello: <%= a %>");
console.log(compiled({a: 'moe'}));
compiled=\.template(“hello:”);
log(编译({a1:'moe'}));
compiled=\模板(“hello:”);
log(编译({u1:'moe'}));
compiled=\模板(“hello:”);
log(编译({a:'moe'}));