当ejs文件编译时,它就变成了javascript函数。例如,这个ejs文件
<h3> Users Index</h3>
<ul>
<% users.each(function (user) { %>
<li><%= user.get("name") %></li>
<% })%>
</ul>编译到这个javascript函数:
(function() { this.JST || (this.JST = {}); this.JST["users/index"] = function(obj){var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('<h3> Users Index</h3>\n\n<ul>\n '); users.each(function (user) { ; __p.push('\n <li>', user.get("name") ,'</li>\n '); }); __p.push('\n</ul>\n\n');}return __p.join('');};
}).call(this);在上面的javascript函数中,this引用什么?(即)(主干视图等)
发布于 2015-12-22 02:23:19
如果您像这样运行一个自执行函数构造
(function() { console.log(this) }).call(this);你会注意到它会输出(Chrome控制台输出)
Window {external: Object, chrome: Object, document: document, i: undefined, StackExchange: Object…}因此,所有这些都只是将变量JST添加到全局(window)命名空间中,以便在下面的每个脚本中都可以访问它。
行this.JST || (this.JST = {});将确保如果window.JST (如我们所见的window= this )存在,如果不存在,则创建它,它的值将是一个空对象(this.JST = {})。
https://stackoverflow.com/questions/34407494
复制相似问题