0

Possible Duplicate:
Why is JSHINT complaining that this is a strict violation?

In the abbreviated code below I use this function for determining if a function argument is an element id or an object. This way I always have the element to work with.

function domPageFlip( some_var )
{
    var select_element;

    if( typeof ( some_var ) === 'string' ) // internally called
    {
        select_element = document.getElementById( some_var );
    }
    else // called by user event ( via event listener )
    {
        select_element = this;
    }

jshint gives a possible strict violation. How do I write the code so I do not get this violation?

Community
  • 1
  • 1
CS_2013
  • 1,158
  • 3
  • 13
  • 24
  • not a duplicate as my use of this is obviously inside a method/function, and this is not undefined. – CS_2013 May 20 '12 at 20:05
  • 4
    @CS_2013 - Your use is obviously inside a *function*, but it doesn't look like it's inside a *method*. – James Allardice May 20 '12 at 20:06
  • @CS_2013 Maybe check out what it's saying again? – Dave Newton May 20 '12 at 20:06
  • It is bound ( via an eventListener ) to an object ( p element ) sometimes when it is used, and sometimes it is not ( intenally called ) – CS_2013 May 20 '12 at 20:06
  • 2
    @CS_2013 - Well there you go. If it's not bound to an object then `this` refers to the window and you get your possible strict mode violation. – James Allardice May 20 '12 at 20:07
  • Semantics...syntax is the same..sometimes and event calls it some times the page calls it..but O.K...what do I do now. – CS_2013 May 20 '12 at 20:08
  • I don't use this if it's not bound to an object so what's the issue? ( that is why it's in an else statement ) – CS_2013 May 20 '12 at 20:09
  • why can't jshint tell that it is bound to an object at times. – CS_2013 May 20 '12 at 20:11
  • "*possible* strict violation" – glortho May 20 '12 at 20:11
  • @CS_2013 - If your code works, then fine, there's no problem and you can ignore the warning. Note that JSHint is only saying *possible* violation. And you yourself are saying that the function isn't always bound to an object (in which case, if `some_var` was not a string, you would have an *actual* violation). – James Allardice May 20 '12 at 20:12
  • Because jshint doesn't know all the contexts in which it might be called, hence the "possible". Static analysis of such a dynamic language is hard. – Dave Newton May 20 '12 at 20:12
  • O.K. I'm trying to write jshint.com perefect code for peace of mind...I'll work something out here. – CS_2013 May 20 '12 at 20:13
  • I will try to make two variables in my param list ...see if this "fixes" it...and if called by internal just set the "this" var to null – CS_2013 May 20 '12 at 20:15
  • 1
    @CS_2013 - If you insist on having no warnings then make the non-bound version a separate function and call that instead of this one when necessary. The problem is simply the use of `this` in a function that doesn't appear to be bound to anything. Although I'm not sure if even that will fix the warning, since the function still may not appear to be bound to anything. – James Allardice May 20 '12 at 20:15
  • But why does it not appear to be set anything by the static analyzer of jshint.com when INDEED it is. -> That's another question: http://stackoverflow.com/questions/10677535/jshints-static-analysis-does-not-detect-use-of-method-and-assoicated-this-why – CS_2013 May 20 '12 at 21:55
  • You could try: `if (typeof this == 'undefined') { /* error */ } else { ...}` or explicitly pass either an object or string to the function. – RobG May 20 '12 at 23:24

0 Answers0