JSHint 및 jQuery : '$'이 (가) 정의되지 않았습니다

JSHint 및 jQuery : '$'이 (가) 정의되지 않았습니다

JSHint 및 jQuery : '$'이 (가) 정의되지 않았습니다

다음 JS :

(function() { "use strict"; $("#target").click(function(){ console.log("clicked"); }); }());

수율 :

test.js: line 5, col 3, '$' is not defined.

JSHint를 사용하여 보풀 일 때 0.5.5. 어떤 아이디어?

비교적 최신 버전의 JSHint를 사용하는 경우 일반적으로 선호되는 접근 방식은 프로젝트의 루트에 .jshintrc 파일을 작성하고이 구성을 파일에 넣는 것입니다.

{ "globals": { "$": false } }

이는 $가 전역 변수임을 JSHint에 선언하고 false는 대체하지 않아야 함을 나타냅니다.

.jshintrc 파일은 실제 JSHint 이전 버전 (예 : 2012 년의 원래 질문과 같은 v0.5.5)에서는 지원되지 않았습니다. .jshintrc 파일을 사용할 수 없거나 사용하지 않으려면 스크립트 파일 맨 위에이를 추가 할 수 있습니다.

/*globals $:false */

JSHint 옵션 페이지 에서 볼 수 있듯이 속기 "jquery"jshint 옵션도 있습니다 .

.jshintrc에 두 줄을 추가 할 수도 있습니다

"globals": { "$": false, "jQuery": false }

이것은 jshint에게 두 개의 전역 변수가 있음을 알려줍니다.

당신이해야 할 모든에 설정 "jquery": true 되어 있습니다 .jshintrc .

당 JSHint 옵션 참조 :

jquery 이 옵션은 jQuery JavaScript 라이브러리에 의해 노출되는 글로벌을 정의합니다.

다음은 .jshintrc에 넣을 행복한 작은 목록

입니다. 시간이 지나면이 목록에 추가 할 것입니다.

{ // other settings... // ENVIRONMENTS // "browser": true, // Is in most configs by default "node": true, // others (e.g. yui, mootools, rhino, worker, etc.) "globals": { "$":false, "jquery":false, "angular":false // other explicit global names to exclude }, }

WebStorm, PyCharm, RubyMine 또는 IntelliJ IDEA와 같은 IntelliJ 편집기를 사용하는 경우 :

파일 / 설정 / 자바 스크립트 / 코드 품질 도구 / JSHint의 환경 섹션에서 jQuery 체크 박스를 클릭하십시오.

일반적인 "JSHint 전역 끄기"를 권장하는 대신 모듈 패턴 을 사용하여이 문제를 해결 하는 것이 좋습니다 . 그것은 코드를 "포함"하고 유지 성능을 향상시킵니다 (Paul Irish의 "Jquery에 대해 배운 10 가지 사항" 기반 ).

나는 다음과 같이 모듈 패턴을 쓰는 경향이 있습니다.

(function (window) { // Handle dependencies var angular = window.angular, $ = window.$, document = window.document; // Your application's code }(window))

다음과 같은 다른 성능 이점을 얻을 수 있습니다 (자세한 내용은 여기 참조 ).

코드를 축소하면 전달 된 window 객체 선언도 축소됩니다. 예를 들면 window.alert() 된다 m.alert() .

자체 실행 익명 함수 내부의 코드는 window 개체의 인스턴스 하나만 사용 합니다.

You cut to the chase when calling in a window property or method, preventing expensive traversal of the scope chain e.g. window.alert() (faster) versus alert() (slower) performance.

property or method, preventing expensive traversal of the scope chain e.g. (faster) versus (slower) performance. Local scope of functions through "namespacing" and containment (globals are evil). If you need to break up this code into separate scripts, you can make a submodule for each of those scripts, and have them imported into one main module.

If you're using an IntelliJ editor, under

Preferences/Settings Javascript Code Quality Tools JSHint Predefined (at bottom), click Set

You can type in anything, for instance console:false , and it will add that to the list (.jshintrc) as well - as a global.

To fix this error when using the online JSHint implementation:

Click "CONFIGURE" (top of the middle column on the page)

Enable "jQuery" (under the "ASSUME" section at the bottom)

참고URL : https://stackoverflow.com/questions/8852765/jshint-and-jquery-is-not-defined

from http://big-blog.tistory.com/2480 by ccl(A) rewrite - 2020-04-29 23:27:44