your programing

JSLint 메시지 : 사용되지 않는 변수

lovepro 2020. 10. 10. 10:47
반응형

JSLint 메시지 : 사용되지 않는 변수


JSLint가 이러한 시나리오에서 사용되지 않는 변수 인 "i"에 대해 불평하면 어떻게해야합니까?

var items = "<option selected></option>";
$.each(data, function (i, item) {
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

(i, item)은 매개 변수의 필수 순서이며 "item"만 사용하고 있습니다.

사용하지 않는 변수를 허용하거나 색인을 사용하기 위해 $ .each를 다시 작성하는 것 외에 다른 해결책이 있습니까?

미리 감사드립니다.

업데이트 : 모든 제안에 감사 드리지만이 코드는 단순히 내가 의미하는 바를 보여주는 예제 일 뿐이며 일반적인 해결책이 있다면 관심이 있습니다. 감사.


시험:

var items = "<option selected></option>";
/*jslint unparam: true*/
$.each(data, function (i, item) {
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});
/*jslint unparam: false*/  // so that you still get warnings from other functions

http://www.jslint.com/help.html 에서 이것이 새로운 것 같습니다 .

"JSLint는 새로운 예약어를 도입했습니다 : ignore"

따라서 위의 내용은 다음과 같습니다.

$.each(data, function (ignore, item) {

나는 => 무시 ... 너무 쉽습니다. 나머지 코드는 동일하게 유지 될 수 있으며 브라우저는 만족스럽고 JSLint는 만족합니다.


이전 (잘못된) 답변 :

JsLint와 브라우저를 모두 게시하려면 다음을 사용해야합니다.

function (d, i) {
        if (undefined !== win.undefined) {
            undefined(d);
        }
        return (i);
}

undefined가 함수가 아니기 때문에 브라우저가 "undefined (d)"에서 충돌했습니다. 따라서 "undefined! == win.undefined"는 브라우저에있는 경우 줄을 건너 뜁니다.


당신은 이것을 할 수 있습니다 :

var items = "<option selected></option>";
$.each(data, function () {
    var item = arguments[1];
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

...하지만 나에게 물어 보면 더 나쁠 것입니다.


상당히자가 문서화 방식으로 경고를 제거하는 가능한 방법은 다음과 같이 사용되지 않는 변수를 사용하도록하는 것입니다.

// Utility function in project scope:
function unusedVariables(/* Put all your deliberately unused variables here */) {
    // pass
}

// And then, later:
var items = "<option selected></option>";
$.each(data, function (i, item) {
    unusedVariables(i); //< This is the new and magical line
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

물론 이제 변수를 사용하지 않음으로 표시하고 여전히 어딘가에서 사용하는 상황에 들어갈 수 있습니다. 또한이 방법은 컨텍스트에 따라 너무 장황 할 수 있습니다.

이 방법은 정확하다는 장점이 있습니다. 사용 범위 /*jslint unparam*/가 너무 넓을 수 있습니다.


void의도적으로 변수를 사용하지 않음을 명시하기 위해 사용 하는 것은 어떻습니까?

$.each(data, function (i, item, any, other, unused, vars) {
  void(i, any, other, unused, vars);
  items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

This is also useful in abstract functions that are expected to be overwritten, but where you want to show the signature, or in mocks, where you are ignoring arguments, but want to match the mocked function's signature.


I rename "i" as "unused". It still leaves the error obviously, but I see it in the list and know I've "checked" that error and am okay with it.


In this particular case of transforming an array/object http://api.jquery.com/jquery.map/ (or http://api.jquery.com/map/ ?) is an option.

var items = "<option selected></option>" + $.map(data, function (item) { 
    return "<option value='" + item.Value + "'>" + item.Text + "</option>";
}).get().join('');

If function has more than one unused parameters, you can use "ignore" like this :

function (ignoreFoo, ignoreBar, baz) {
}

It must simply begin with the reserved word "ignore" (ignore, ignoreFoo, ignoreBar, ...).

참고URL : https://stackoverflow.com/questions/6583623/jslint-message-unused-variables

반응형