本文介绍了Jshint.com |分配给"this"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在jshint.com中

In jshint.com whenever I do

var this_hold = this;

我得到一个错误.

我违反了严格的错误.

应用程序是如此,我需要使用它(从事件处理程序中传入),或者我需要使用document.getElementById()

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.

我希望错误消失.我不想将其关闭.我希望jshint.com 100%满意.

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

推荐答案

您可以使用选项validthis关闭该警告.不幸的是,您不能仅通过进行静态分析就检测到所有严格的模式违例,因此JSHint会做出猜测-有时是错误的.

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.

有关validthis和我们文档中其他选项的更多信息: http://www.jshint.com/docs /

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

这篇关于Jshint.com |分配给"this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:07