javascript - 在 javascript 中使用 this 关键字时出现严重违规是什么意思?

标签 javascript jquery this jslint strict-mode

有人可以给我建议吗?我刚开始使用 jslint,我收到一条错误消息“strict violation”指向带有“dialog(this)”的行 -

function accessLinkClick(e) {
    e.preventDefault();
    $('.accessLink')
    .unbind('click', accessLinkClick);
    dialog(this);
}

function accessControls() {
    $('.accessLink')
        .bind('click', accessLinkClick);
    $('#logoutLink')
        .click(function (e) {
            window.location = $(this).attr('data-href');
        });
}

我以前从未见过这条消息。这是什么意思?

最佳答案

规范的附件 C 是这样解释的:

If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4).

在上述代码中使用e.target代替this

引用资料

关于javascript - 在 javascript 中使用 this 关键字时出现严重违规是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12603340/

相关文章:

jquery - 如何使用 jQuery 选择非直接左兄弟 <span> ?

Jquery $(this) 不工作

javascript - $(this) 和 this 有什么区别

javascript - 在 javascript 原型(prototype)事件处理程序中保留 'this' 引用

javascript - 使用 javascript 或 jquery 获取图像大小

jquery - 使用 jquery 加载 smarty tpl

javascript - (this).attr() 在 jquery.noConflict() 之后停止工作

javascript - 数据表选择 - 禁用几行以供选择

javascript - 如何在 Universal Analytics 中按页面类型生成报告

javascript - 如何制作与横幅背景相同的固定标题?