javascript - jQuery callback - strict violation - Stack Overflow

I get the basic idea about this not being in a method when in strict mode outlined here, but it gets a

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
});

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. 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.

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 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
});

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. 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.

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

Share Improve this question edited May 23, 2017 at 11:58 CommunityBot 11 silver badge asked Jul 26, 2012 at 8:07 NickNick 6,03512 gold badges56 silver badges80 bronze badges 2
  • The accepted answer to the question you link to suggests to replace the function statement with a function expression. Did you try that? – Frédéric Hamidi Commented Jul 26, 2012 at 8:12
  • There's nothing wrong with using this inside a function. Given that "methods" don't really belong to objects (they're just properties that refer to functions defined somewhere, and possibly shared with other objects) I don't see what the objection is. – nnnnnn Commented Jul 26, 2012 at 8:14
Add a ment  | 

4 Answers 4

Reset to default 6

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. They're largely unrelated (except that JSLint has an option to require strict mode).

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 doesn't like it, but JSLint doesn't like a lot of things that are perfectly valid and appropriate. JSLint detects a bination 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 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.

Can't you use the event object instead?

i.e.

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

Example: http://jsfiddle/gRoberts/cRnb3/

you could read the currentTarget of the event-object passed trough, like:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744771109a4592764.html

相关推荐

  • javascript - jQuery callback - strict violation - Stack Overflow

    I get the basic idea about this not being in a method when in strict mode outlined here, but it gets a

    11小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信