6

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?

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78
  • 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 Jul 26 '12 at 08: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 Jul 26 '12 at 08:14

4 Answers4

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • This is really helpful. I use Aptana's built-in JSLint, and the error "strict violation" just sounded so... authoritative :) – Nick Jul 26 '12 at 08:20
  • @Nick: Ack! Indeed, I didn't realize it was phrased like that (but a quick check on jslint.com confirms it). Classic Crockford... :-) – T.J. Crowder Jul 26 '12 at 08:21
1

Can't you use the event object instead?

i.e.

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

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

Gavin
  • 6,284
  • 5
  • 30
  • 38
  • Not always, says jQuery docs: `event.target` indicates the deepest (innermost) element where the event occurred, therefore `this` may not be equal to `event.target` if the event has bubbled from a descendant element. – Sam Tsai Oct 14 '15 at 15:05
0

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
Joris W
  • 517
  • 3
  • 16
0

Try this

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

obj.one('keyup', myFunction);
obj.one('keyup', myFunction); etc
Imdad
  • 5,942
  • 4
  • 33
  • 53