jQuery回调 - 严格违反 [英] jQuery callback - strict violation

查看:133
本文介绍了jQuery回调 - 严格违反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在< code> strict中有关于这个不在方法的基本概念模式概述了这里,但它有点博学,老实说。所以,在更平凡的词:

I get the basic idea about this not being in a method when in strict mode outlined here, but it gets a bit erudite, to be honest. So, in more prosaic terms:

我有一个处理程序,如:

I have a handler like so:

$('.myClass').one('keyup', function() {
    var $this = $(this);
    etc etc
});

我想将其更改为:

function myFunction () {
    var $this = $(this);
    etc etc
};
$('.myClass1').one('keyup', myFunction);
$('.myClass2').one('keyup', myFunction); etc

不喜欢,因为在 strict模式,因为我在方法之外使用 this

It doesn't like it because in strict mode because I'm using this outside of a method. I get that.

但是,我需要将 myFunction 独立于处理程序,因为(1)到各种类/元素和(2)我使用 .off()。一个('keyup',myFunction)重置 code>

However, I need to have myFunction separate to the handler, because (1) it's attached to various classes/elements and (2) I am using .off().one('keyup', myFunction) to reset the one handler for various classes at various points.

那么如何绕过一个单独的回调函数,而不违反 this business?

So how do I get around having a separate callback function without violating the this business?

推荐答案


在严格模式下的方法...

I get the basic idea about this not being in a method when in strict mode...

是的,但是你混淆了两个无关的事情:严格模式和JSLint。它们在很大程度上是不相关的(除了JSLint有一个选项要求严格模式)。

Yes, but you're confusing two unrelated things: Strict mode, and JSLint. They're largely unrelated (except that JSLint has an option to require strict mode).

在严格模式下你的代码没有问题。它是完全有效和适当的(因为jQuery如何使用 this )。 示例严格违规。

There's no problem with your code in strict mode. It's perfectly valid and appropriate (because of how jQuery uses this). Example There is no "strict violation" there.

JSLint不喜欢它,但JSLint不喜欢很多东西是完全有效和适当的。 JSLint检测到广泛承认是问题(例如缺少分号)的事情的组合,以及Douglas Crockford从风格角度看不喜欢的事情。 Crockford的聪明和知情,但不是每个人都同意他的风格选择。

JSLint doesn't like it, but JSLint doesn't like a lot of things that are perfectly valid and appropriate. JSLint detects a combination of things that are very widely-acknowledged to be problems (like missing semicolons), and things that Douglas Crockford doesn't like from a style perspective. Crockford's intelligent and well-informed, but not everyone agrees with his style choices.

Lint工具是非常有用的,工具包的一个重要组成部分。 JSLint是不太有用的(在我看来),通过将物质问题与风格问题(虽然被授予它是一个细线,与JavaScript)。您可以考虑使用 JSHint ,这是一个可以更好地控制其检查内容的分支。

Lint tools are very helpful, an essential part of the toolkit. JSLint is less helpful than it might be (in my view), by conflating substance issues with style issues (although granted it's a fine line, with JavaScript). You might consider using JSHint, a fork which gives you more control over what it checks.

这篇关于jQuery回调 - 严格违反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆