编译后的jst模板中的this是什么
what is the `this` in a compiled jst template
当一个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
指的是什么?(即主干视图等)
如果运行类似的自执行函数构造
(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 = {}
)。
相关文章:
- this.someFunction.call(this,param);的用途是什么;
- “var self = this”方法背后的原理是什么?
- 这条线是什么意思"this.parentNode.firstChild.nodeName”;在jQuery H
- 当javascript单词this用作数组时,它的含义是什么
- 在方法中的函数中引用 CoffeeScript 类中的“this”的最佳方式是什么?
- “this._find(回调)”在猫鼬find()方法中是什么意思
- jQuery $('element', this) selector,这是什么意思
- Ember 路由器中的“this._super(控制器,型号)”是什么意思?
- 当作为脚本运行时.js Node中“this”的上下文是什么
- 概念 - 在 addEventListener 回调中,“this”指的是什么
- Javascript中“this”关键字的目的是什么?
- Node.js:在模块作用域中使用“this”运算符的上下文是什么
- 模块模式内的回调函数中的“this”指的是什么
- “this”关键字在下面的Javascript代码中指的是什么
- 编译后的jst模板中的this是什么
- 在Javascript中,在什么条件下只在构造函数中使用“this”是合适的
- 这个onclick(onclick=“this.name='abc'”)是什么意思
- 当用作函数参数/agrument时,关键字this指的是什么
- '的语义是什么;this.object.position'在Three.JS's轨迹球
- $(">", this)是什么意思[1]