“use strict”和 underscore.js 的问题

2024-05-15

我使用 Yeoman 和backbone.js 编写了一个应用程序。在我指定的每个 js 文件的顶部'use strict';当我运行 grunt 任务时,jshint 不会遇到任何错误。

我可以使用 grunt 构建我的应用程序而不会出现问题,但是当我尝试运行丑化的 js 时,出现以下错误:

Uncaught SyntaxError: Strict mode code may not include a with statement

我搜索了代码库,唯一使用 with 语句的是下划线。

我是严格模式的新手,所以我不确定如何解决这个问题。我可以在使用 underscorejs 函数的地方不使用严格模式吗?

Thanks.

EDIT:

给出下面的代码示例(为了简洁而缩短)。我怎样才能改变它来解决这个问题。

'use strict';

/*global, Backbone, JST*/

var MyView = Backbone.View.extend({

    template: JST['app/scripts/templates/MyView.ejs'],

    initialize: function()
    {
        this.render();
    },

    render : function()
    {
        this.$el.html(this.template(this.templateVariables()));
        return this;
    },

    templateVariables: function()
    {
        return {var1 : 'Hello', var2 : 'World'};
    }
});

在 MyView.ejs 中

<p><%= var1 %><%= var2 %>!</p> //<p>Hello World!</p>

EDIT 2:

使用 @mu 太短裤的答案如下我发现解决对 _.template 的调用的最佳方法是更改​​我的 grunt-JST 任务,如下所示:

jst: {
        compile: {
            options:
            {
                templateSettings:
                {
                    variable: 'data'
                }
            },
            files: {
                '.tmp/scripts/templates.js': ['<%= yeoman.app %>/scripts/templates/*.ejs']
            }
        }
    },

然后更改我的每个模板以使用<%= data.templateVariable %> format.

可能不适用于其他人,但我在使用 Yeoman 和 Grunt 以及 Backbone 生成器时遇到了这个问题,所以我不是唯一的一个。


下划线的_.template uses with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with在内部允许类似的事情<%= pancakes %>待解决obj.pancakes。如果你看看里面_.template https://github.com/jashkenas/underscore/blob/1.5.2/underscore.js#L1199,你会发现这个:

if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';

这就是进攻的地方with来自。如果您使用 JST 风格的预编译模板,那么source就是你最终会得到的JST对象,这使得withs 范围内可见"use strict"。请注意settings.variable在那里?文档 http://underscorejs.org/#template says:

默认情况下,template通过以下方式将数据中的值放置在本地范围内with陈述。但是,您可以使用以下命令指定单个变量名称variable环境。这可以显着提高模板的渲染速度。

_.template("Using 'with': <%= data.answer %>", {answer: 'no'}, {variable: 'data'});
=> "Using 'with': no"

所以你可以抑制with通过使用variable编译模板时的选项;当然,这也意味着你必须重写所有的<%= ... %>模板的一部分以匹配variable选项必须说(这也应该加速你的模板,所以它可能是值得的)。

对于您的情况,您可以将模板更改为:

<p><%= data.var1 %><%= data.var2 %>!</p> //<p>Hello World!</p>

然后你需要改变_.template用于编译模板的调用如下所示:

var compiled_template = _.template(raw_template, null, { variable: 'data' });

你不必使用data当然,您只需要在模板和应用程序中使用相同的东西即可_.template call.

我不知道你会如何改变你的设置通话方式_.template但这不应该那么困难。我想你可以猴子补丁_.template有一个默认值variable作为最后的手段。

这是一个简单的演示,可以说明正在发生的事情:http://jsfiddle.net/ambigously/Az8QM/ http://jsfiddle.net/ambiguous/Az8QM/


或者,如果我们看看如何"use strict"已确定范围 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode#Invoking_strict_mode,我们会看到:

严格模式适用于整个脚本 or to 个别功能.

所以你可以用这样的东西来定位你的严格性:

(function() {
    "use strict";
    // All your non-JST JavaScript goes here.
})();
// Append your JST out here.

您还可以使用两个 JavaScript 文件,而不是仅使用一个:

  1. 一款适用于您的非模板 JavaScript"use strict"已启用。
  2. 第二个只有你的JST, 这个会not "use strict".
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“use strict”和 underscore.js 的问题 的相关文章

随机推荐