2

Can someone give me advice with this. I just started using jslint and I got an error message "strict violation" pointing to the line with "dialog(this)" -

function accessLinkClick(e) {
    e.preventDefault();
    $('.accessLink')
    .unbind('click', accessLinkClick);
    dialog(this);
}

function accessControls() {
    $('.accessLink')
        .bind('click', accessLinkClick);
    $('#logoutLink')
        .click(function (e) {
            window.location = $(this).attr('data-href');
        });
}

I never saw this message before. what does it mean?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • See also http://stackoverflow.com/questions/6300937/strict-violation-using-this-keyword-and-revealing-module-pattern – Michael Berkowski Sep 26 '12 at 13:55
  • why do you pass `this` do the `dialog()` function? `this` in that case refers to the global object (window) so you don't really need to pass it – Vlad Balmos Sep 26 '12 at 13:55
  • 2
    Or http://stackoverflow.com/questions/2500512/jslint-why-do-this-code-result-in-a-strict-violation-error-message – CaffGeek Sep 26 '12 at 13:56
  • @CaffGeek That's a better example than I was able to find – Michael Berkowski Sep 26 '12 at 13:56
  • This post should help: [http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it][1] [1]: http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it – Matt Asbury Sep 26 '12 at 13:57
  • I don't get that message. Do you get it in your browsers error log? – javascript is future Sep 26 '12 at 13:57
  • @javascriptisfuture the message is received while validating the code with [jslint](http://www.jslint.com/) – Vlad Balmos Sep 26 '12 at 13:59
  • @javascriptisfuture, the OP is likely using "use strict", see: http://www.yuiblog.com/blog/2010/12/14/strict-mode-is-coming-to-town/ – CaffGeek Sep 26 '12 at 14:00
  • @VladBalmos keep in mind that he uses `accessControls` as an eventHandler: `.bind('click', accessLinkClick)`. So `this` will be the element that fired the event. – Dennis Sep 26 '12 at 14:12
  • Yes I am using strict mode. Dialog is a function that I have created. It opens a modal window. I need to pass it the information from the object that was clicked as that has data- attributes that are used when making the dialog. – Alan2 Sep 26 '12 at 14:38

1 Answers1

0

Annex C of the spec explains it as such:

If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4).

Use e.target instead of this in the aforementioned code.

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265