我正在阅读一个应用程序的源代码,我看到了这行代码:
$('#div1').html(_.template($('#div2').html())({Id: app.Id(id)}));
我能理解$('#div1').html(),但是为什么这行代码可以传递两个()代码块呢?看起来不对劲。 .html() 可以带两个 () block 吗?
.html(_.template()())
;
最佳答案
这是因为 _.template()
返回一个函数,然后我们使用第二组 ()
调用该返回的函数
var fn = _.template(sometemplate);//it gives a parsed template which is a function
fn(data);//it merges the data and the template to generate the html
关于javascript - 此代码的工作原理 .html(_.template()({ }) ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371240/