3

In jshint.com whenever I do

var this_hold = this;

I get an error.

I get a violation of strict error.

The application is such that I need to work with either this ( passed in from an event handler) or I need to pull the element myself using document.getElementById()

It's just how this function works best...otherwise I have to write it twice once for each case.

I want the error gone..I don't want to turn it off. I want jshint.com 100% happy.

Here is the function in question with the violation commented

/**
 *vFlipBP - eliminate select_element in favor of 'this'
 */

function vFlipBP( element_or_string ) {
    var previous_page_element,
        previous_tag_element,
        current_page_element,
        select_element;
    console.log( 'element_or_string ' + element_or_string ); 
    if( typeof ( element_or_string ) === 'string' ) {
        select_element = document.getElementById( element_or_string );
    } else {
        select_element = this;  // violation of strict here
    }
    if( vFlipBP.previous_id === undefined ) {
        var probe_id = select_element.parentElement.firstChild.id;
        if ( ( probe_id === select_element.id ) && ( select_element.parentElement.firstChild.nextSibling ) ) {
            probe_id = select_element.parentElement.firstChild.nextSibling.id;
            vFlipBP.previous_id = probe_id;
        } else {
            vFlipBP.previous_id = select_element.id;
        }
    }
    current_page_element = document.getElementById( select_element.id + '_page' );
    current_page_element.style.display = '';
    select_element.style.background = "#eeeeee";
    if( vFlipBP.previous_id !== select_element.id ) {
        previous_page_element = document.getElementById( vFlipBP.previous_id + '_page' );
        previous_tag_element = document.getElementById( vFlipBP.previous_id );
        if( ( ( previous_page_element !== current_page_element ) ) && ( previous_page_element !== null ) ) {
            previous_page_element.style.display = 'none';
            previous_tag_element.style.background = "#ffffff";
        }
    }
    vFlipBP.previous_id = select_element.id;
}

2 Answers2

1

You can't assign to this; it's read-only.

However, assigning the value of this to another variable is not erroneous, and it does not violate "use stict" rules.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Line 15: select_element = this; Possible strict violation. –  Aug 13 '12 at 20:21
  • @HiroProtagonist is that from JSLint or something? If so, JSLint is really dumb; it makes goofball errors like that a lot. Basically it's not keeping track of the fact that the variable was correctly declared with `var`. – Pointy Aug 13 '12 at 20:22
  • Unfortunately those analysis tools have very weak parsers. That's why it says "**Possible** strict violation." – Pointy Aug 13 '12 at 20:22
  • 1
    Hey JSHint maintainer(s) sorry about the "weak" comment :-) – Pointy Aug 13 '12 at 20:34
1

You can use an option validthis to turn off that warning. Unfortunately, you can't detect all strict mode violations just by doing static analysis so JSHint makes a guess—and sometimes it is wrong.

More about validthis and other options in our docs: http://www.jshint.com/docs/

Anton Kovalyov
  • 2,808
  • 1
  • 22
  • 11
  • There is no need to be proud of not using options. JSHint is not there to tell you how to code—it is there to help you spot potential problems. And since static analysis can get you only so far we will always have false positives. – Anton Kovalyov Aug 13 '12 at 21:24
  • Well...I finally did it anyways...not options set...no errors...there were multiple fixes actually...see post here....http://stackoverflow.com/questions/11961735/jshint-passing-default-settings-function-declarations-and-this. –  Aug 16 '12 at 23:56