2016-08-18 1 views
-2

이 코드 생성기를 빌드 할 때 도움이 필요합니다. 선생님이 JSHint에서 확인하면 5 개의 정의되지 않은 변수가 나타납니다. 객체 또는 참조 문자열을 저장하고 있습니다. 당신이 아래에서 볼 수 있듯이 커뮤니티가 나에게 어떤 수정 제안을 해주 었는지 제안했는데, 잘 작동하지만 왜 jshint에서 그것을 확인하는지 모르겠다. 나는 편집자를 위해 자신의 숭고한 텍스트와 intellyJ를 사용한다. JShint가 보여주는 오류, 정의되지 않은 변수 5 개, 콘솔에 오류 에테르가 표시되지 않습니다.그래서이 코드 생성기를 빌드하고 최대 파가 아니야

var quotes = [ 
quoteOne = { 
     quote : 'I meant what I said and I said what I meant.', 
     source : 'Dr. Seuss', 
     citation : 'dont know', 
     year : '1990' 
    }, 
     quoteTwo = { 
     quote : 'Truth is everybody is going to hurt you: you just gotta find the  ones worth suffering for.', 
     source : 'Bob Marley', 
     citation : 'Smoke session', 
     year : '1989' 
    }, 
     quoteThree = { 
     quote : 'Never interrupt your enemy when he is making a mistake.', 
     source : 'Napoleon Bonaparte', 
     citation : 'I dont know', 
     year : '1991' 
    }, 
     quoteFour = { 
     quote : 'Fear is the main source of superstition, and one of the main sources of cruelty. To conquer fear is the beginning of wisdom.', 
     source : 'Bertrand Russell', 
     citation : 'I dont know', 
     year : '1992' 
    }, 
    quoteFive = { 
     quote : 'Curiosity will conquer fear even more than bravery will.', 
     source : 'James Stephens', 
     citation : 'I dont know', 
     year : '1993' 
    }]; 
//this function picks a quote in random from my quote storage and returns how to print on the page. 
    function getRandomQuote() { 
     var item = quotes[Math.floor(Math.random()* quotes.length)]; 
     return '<p class="quote">' + item.quote + '<p class="source">' + item.source + '<span class="citation"><br>' + item.citation + '<br><span class="year"><br>' + item.year + ', ' ; 
    } 
//this function prints the quote when the button is click 
    function printQuote() { 
     document.getElementById('quote-box').innerHTML = getRandomQuote(); 
     document.getElementById('loadQuote').addEventListener("click", printQuote, false); 
    } 

    console.log(printQuote()); 

답변

0

5 개 정의 변수 어레이 선언 quote1, quote2 ... quote5이다. 내가 원래 코드가 자바 스크립트 유효 놀랍군요

var quotes = [ 
    { 
     quote : 'I meant what I said and I said what I meant.', 
     source : 'Dr. Seuss', 
     citation : 'dont know', 
     year : '1990' 
    }, 
    { 
     quote : 'Truth is everybody is going to hurt you: you just gotta find the  ones worth suffering for.', 
     source : 'Bob Marley', 
     citation : 'Smoke session', 
     year : '1989' 
    }, 
    { 
     quote : 'Never interrupt your enemy when he is making a mistake.', 
     source : 'Napoleon Bonaparte', 
     citation : 'I dont know', 
     year : '1991' 
    }, 
    { 
     quote : 'Fear is the main source of superstition, and one of the main sources of cruelty. To conquer fear is the beginning of wisdom.', 
     source : 'Bertrand Russell', 
     citation : 'I dont know', 
     year : '1992' 
    }, 
    { 
     quote : 'Curiosity will conquer fear even more than bravery will.', 
     source : 'James Stephens', 
     citation : 'I dont know', 
     year : '1993' 
    }]; 

- 그래서 내가 뭔가를 배운 오늘 : 다른 모든 것은 나에게 좋아 보인다 : 같은

귀하의 배열이 보일 것입니다.

+0

그래, 나는 여러 가지 방법으로 고양이를 피부에 이긴다는 것을 알고있다. 그러나 그렇다. 문제는 아니었다. 나는 JSHint에 관한 버그를보고했다. 백엔드에서 무언가를 고쳤다. 이제는 코드가 구문에 오류를 나타내지 않거나 정의되지 않았다. 하지만 코드가 다르고 멋지다는 많은 사람들이 있습니다. 코딩은 예술의 한 방법이고, 흐르는 방법은 놀랍습니다. 그리고 틀린 것처럼 보이는 희귀 한 코드를 배치하는 새로운 방법을 시도하지만 유효합니다. –

+0

명확히 말하면, 나는 내 재량에 대한 부정적인 답변을하려고하지는 않았다 : 유효한 자바 스크립트. 어쨌든 JSHint는 5 개의 정의되지 않은 변수에 플래그를 지정하는 것이 옳았습니다. - quote1' ...'quote5'는 코드의 아무 곳에서나 정의되지 않았으므로 전역 변수로 할당되었습니다 (나쁜 것임) –

관련 문제