简体   繁体   English

jQuery回调 - 严格违规

[英]jQuery callback - strict violation

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. 我得到的基本想法是,在这里概述的strict modethis不是一种method ,但说实话,它有点博学。 So, in more prosaic terms: 所以,用更平淡的术语:

I have a handler like so: 我有一个像这样的处理程序:

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

I want to change it to this: 我想把它改成这个:

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

It doesn't like it because in strict mode because I'm using this outside of a method. 它不喜欢它,因为在strict mode因为我在方法之外使用this I get that. 我明白了。

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. 然而,我需要有myFunction分离到该处理程序,这是因为(1)它的附连到各种类别/元素和(2)我使用.off().one('keyup', myFunction)重置one关于各种处理程序各个阶段的课程。

So how do I get around having a separate callback function without violating the this business? 那么如何在不违反this业务的情况下绕过单独的回调函数呢?

I get the basic idea about this not being in a method when in strict mode... 在严格模式下,我得到的基本想法是不在方法中......

Yes, but you're confusing two unrelated things: Strict mode, and JSLint. 是的,但是你混淆了两个不相关的东西:严格模式和JSLint。 They're largely unrelated (except that JSLint has an option to require strict mode). 它们在很大程度上是不相关的(除了JSLint可以选择要求严格模式)。

There's no problem with your code in strict mode. 您的代码在严格模式下没有问题。 It's perfectly valid and appropriate (because of how jQuery uses this ). 它是完全有效和适当的(因为jQuery如何使用this )。 Example There is no "strict violation" there. 示例那里没有 “严格违规”。

JSLint doesn't like it, but JSLint doesn't like a lot of things that are perfectly valid and appropriate. JSLint不喜欢它,但JSLint并不喜欢很多完全有效且合适的东西。 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. JSLint检测到一些被广泛认可的问题组合(如缺少分号),以及Douglas Crockford从风格角度看不喜欢的东西。 Crockford's intelligent and well-informed, but not everyone agrees with his style choices. Crockford聪明且消息灵通,但不是每个人都同意他的风格选择。

Lint tools are very helpful, an essential part of the toolkit. Lint工具非常有用,是工具包的重要组成部分。 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). 通过将实体问题与样式问题混为一谈,JSLint的帮助不如它(在我看来)(尽管使用JavaScript认为它很好)。 You might consider using JSHint , a fork which gives you more control over what it checks. 您可以考虑使用JSHint ,它可以让您更好地控制它检查的内容。

Can't you use the event object instead? 你不能使用事件对象吗?

ie

function myFunction (e) {
    var $this = $(e.currentTarget);
};

Example: http://jsfiddle.net/gRoberts/cRnb3/ 示例: http//jsfiddle.net/gRoberts/cRnb3/

you could read the currentTarget of the event-object passed trough, like: 你可以读取传递的事件对象的currentTarget,如:

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

Try this 试试这个

function myFunction () {
    var $this = $(this);
    etc etc
};
var obj=  $('.myClass1');
obj.myFunction =myFunction ;

obj.one('keyup', myFunction);
obj.one('keyup', myFunction); etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM