0

It's difficult to describe the application but basically I need to send 'this' as a parameter instead of it being available using 'this' from with in the function. Here is the case:

paragraph_element.addEventListener( "click", vFlipBP );

Here is part of vFlipBP

/**
 *vFlipBP
 */

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;
    }

.
.
.

The issue it that even though this is valid JavaScript....jshint.com alert to "possible strict violation"

I just prefer no warnings....and don't want to have to configure jshint.com.

I want my code to pass 100% with out having to mess with configuring.

Picky...but just how I want it.

  • Is there some striking, compelling reason you can't just name it something else? – Wug Aug 13 '12 at 20:52
  • 4
    The first argument to an event handler is the event object, your code doesn't make sense. – Esailija Aug 13 '12 at 20:53
  • 1
    @HiroProtagonist it makes little sense to make changes to perfectly good code in order to work around bugs in a tool. – Pointy Aug 13 '12 at 20:54
  • @HiroProtagonist: You want to pass an argument called this. Why not call it "foo"? or "derp"? It doesn't care what it's called. – Wug Aug 13 '12 at 20:54
  • I'm thoroughly confused myself. The only place I see `this` being used is in a simple assignment, nothing to do with any parameter. – KRyan Aug 13 '12 at 20:55
  • [This question he asked only a few minutes ago should help clarify the situation.](http://stackoverflow.com/questions/11941627/jshint-com-assigning-to-this) – Pointy Aug 13 '12 at 20:56
  • 1
    3 questions about this same thing? – Esailija Aug 13 '12 at 20:57
  • 3
    Wait, really? The code works fine, there's a bug in the parser, he's already been told about it, and he's wasting more of our time on his OCD? – KRyan Aug 13 '12 at 20:58
  • The parameter call is not typical...I need to pass in the object/element via 'this' using 'call'. The first parameter is not always an event, if you pass in something else, but this is indeed confusing. There is no bug in the jshint.com parser...it correctly sais "possible"...but more importantly it brought attention to this code which is confusing and possibly not good practice....and I meant the actual 'this' not some random variable 'this'....and no each question is about something different but they are related....changing my code to a consistent calling mechanism is a good thing –  Aug 13 '12 at 23:39

2 Answers2

3

If you must change your code, just do this:

function vFlipBP( element_or_string ) {
    var previous_page_element,
        previous_tag_element,
        current_page_element,
        select_element = this;
    console.log( 'element_or_string ' + element_or_string ); 
    if( typeof ( element_or_string ) === 'string' ) {
        select_element = document.getElementById( element_or_string );
    } 

The mistake you're making is in placing so much value in a "clean" report from a not-so-mature tool like JSHint. Turn on "use strict" in your function(s) and let the actual JavaScript runtime complain.

edit — wait my assumption about what JSHint was griping about may be wrong, according to an answer @minitech gave to the original incarnation of this question. If it's really the case that JSHint is worried that this may be null, then I think it's even sillier to worry about the warning.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • @HiroProtagonist well fine, but you still may find the JSHint has no idea what's going on. – Pointy Aug 13 '12 at 21:06
  • and if I some how where to change my function to a "constructor function" - new vFlipBP(), then that would remove the warning...basically it expects a constructor function when it sees this... –  Aug 14 '12 at 21:56
  • That's strange; I don't know why it'd bother to issue a warning like that, since in JavaScript it's hard (often impossible) to do the analysis that can determine when an expression may be null. – Pointy Aug 14 '12 at 22:12
  • I'm not sure what you mean...what I was trying to say is that jshint.com expects 'this' to be used in conjunction with 'new', which kind of makes sense as in compiled languages this is usually associated with an instantiated ( new ) object. –  Aug 14 '12 at 22:15
  • Well yes, but there are many other situations in which `this` is used, like event handlers and things like that. I rarely use `new` but my code references `this` all the time. – Pointy Aug 14 '12 at 22:19
  • Yep...it is personal preference....I mean it is an option that can be turned off...up to the person. –  Aug 14 '12 at 22:26
2

In javascript there are two ways to explicitly state to what this will point to within the method when it gets called: apply and call.

vFlipBP.apply(objectYouWantToBeThis, [element_or_string]); 

or

vFlipBP.call(objectYouWantToBeThis, element_or_string); 
Erin Stanfill
  • 1,278
  • 11
  • 18