admin管理员组文章数量:1391805
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?
- 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
4 Answers
Reset to default 6I 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
本文标签: javascriptjQuery callbackstrict violationStack Overflow
版权声明:本文标题:javascript - jQuery callback - strict violation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744771109a2624344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论