ドメインモデル
var Question = function(text, answerType){
this.id = 0
this.text = text;
}
var Form = function(){
this.questions = []
this.addQuestion = function(question){
question.id = this.questions.length + 1
this.questions.push(question)
}
}
フォームをレンダリングします
var FormRenderer = function(){
this.wrapperSelector = "#wrapper"
this.render = function(form){
// how to access renderQuestion member of FormRenderer within the higher-order function?
$(form.questions).each( function(){ **this.renderQuestion(form, this) }**)
}
this.renderQuestion = function(form, question){
var questionDomId = "question" + question.id
var questionText = '<input type="text" size="75" name="questionText" value="'+ question.text +'" /><br/><br/><br/>'
var questionWrapper = "<div id='" + questionDomId + "'>" + questionText + "</div>"
// how do i access wrapperSelector member of FormRender when renderQuestion is called as higher-order function?
**$(this.wrapperSelector).append(questionWrapper)**
}
}
クライアントコード
var q1= new Question("question1", "Text Box")
var form = new Form()
form.addQuestion(q1)
var formRenderer = new FormRenderer()
formRenderer.render(form)
質問はタイトルと同じです。上記の特定の例について、Javascriptのコメントで助けを求めました。どんな助けでも大歓迎です。