2011-04-22 6 views
1

나는 기본적인 자바 스크립트 퀴즈를 만들 수있는 예제로이 코드를 주어졌다 :기본 자바 스크립트 퀴즈

var Question = function(id, text, correct, incorrect) { 
    this.id = id; 
    this.text = text; 
    this.correct = correct; 
    this.isCorrectlyAnswered = false; 
    this.all = incorrect; 
    this.all.push(correct); 
    this.all.sort(randomSort); 
} 

Question.prototype.write = function() { 
    var qu = document.getElementById("questions"); 
    qu.innerHTML += "<p>"+this.text+"</p>"; 

    var questionList = "<ul class='questionlist'>"; 
    for (var i=0; i<this.all.length; i++) { 
     if (this.all[i] == this.correct) { 

      questionList += "<li><label onclick='right("+this.id+")'><input type='radio' name='"+this.id+"'>"+this.all[i]+"</input></label></li>"; 

     } else { 

      questionList += "<li><label onclick='wrong("+this.id+")'><input type='radio' name='"+this.id+"'>"+this.all[i]+"</input></label></li>"; 
     } 
    } 
    questionList += "</ul>"; 
    qu.innerHTML += questionList; 
} 

나는이 당황하고있다. 누구든지 '이해할 수있는'청크로 코드를 분해 할 수 있습니까?

+1

꽤 큰 질문 : 당신은 당신이 (약간 이상한) 방식으로 OOP의 작품 나는에게 JS에 대한 자세한 내용을보실 수 있습니다

q.write(); 

기능을 적절한 매개 변수를

var q = new Question(.......); 

말을하고 호출 할 수 있습니다 묻다. 특정 영역을 지적하면 더 유용한 답변을 얻을 수 있습니다. –

+0

특히이 코드에 대해 이해하지 못하는 사항은 무엇입니까? –

+0

배경은 무엇입니까? 자바 스크립트에 대한 경험이 있습니까? 다른 프로그래밍 언어를 사용합니까? 기본 언어 구조 ('if','for','var','this','prototype')를 이해합니까? 객체와 생성자에 대해 아십니까? – Jan

답변

관련 문제