一、

Question是父类,MultipleChoiceQuestion和DragDropQuestion是子类

二、

1.

 <script>
// 面向对象
function Question(theQuestion, theChoices, theCorrectAnswer) {
// Initialize the instance properties​
this.question = theQuestion;
this.choices = theChoices;
this.correctAnswer = theCorrectAnswer;
this.userAnswer = "";
// private properties: these cannot be changed by instances​
var newDate = new Date(),
// Constant variable: available to all instances through the instance method below. This is also a private property.​
QUIZ_CREATED_DATE = newDate.toLocaleDateString();
// This is the only way to access the private QUIZ_CREATED_DATE variable ​
// This is an example of a privilege method: it can access private properties and it can be called publicly​
this.getQuizDate = function () {
return QUIZ_CREATED_DATE;
};
// A confirmation message that the question was created​
console.log("Quiz Created On: " + this.getQuizDate());
} //Add Prototype Methods to The Question Object
// Define the prototype methods that will be inherited​
Question.prototype.getCorrectAnswer = function () {
return this.correctAnswer;
}; Question.prototype.getUserAnswer = function () {
return this.userAnswer;
}; Question.prototype.displayQuestion = function () {
var questionToDisplay = "<div class='question'>" + this.question + "</div><ul>";
choiceCounter = 0;
this.choices.forEach(function (eachChoice) {
questionToDisplay += '<li><input type="radio" name="choice" value="' + choiceCounter + '">' + eachChoice + '</li>';
choiceCounter++;
});
questionToDisplay += "</ul>"; console.log (questionToDisplay);
}; function inheritPrototype(childObject, parentObject) {
// As discussed above, we use the Crockford’s method to copy the properties and methods from the parentObject onto the childObject​
// So the copyOfParent object now has everything the parentObject has ​
var copyOfParent = Object.create(parentObject.prototype); //Then we set the constructor of this new object to point to the childObject.​
// Why do we manually set the copyOfParent constructor here, see the explanation immediately following this code block.​
copyOfParent.constructor = childObject; // Then we set the childObject prototype to copyOfParent, so that the childObject can in turn inherit everything from copyOfParent (from parentObject)​
childObject.prototype = copyOfParent;
}
// Child Questions (Sub Classes of the Question object)
// First, a Multiple Choice Question:
// Create the MultipleChoiceQuestion​
function MultipleChoiceQuestion(theQuestion, theChoices, theCorrectAnswer){
// For MultipleChoiceQuestion to properly inherit from Question, here inside the MultipleChoiceQuestion constructor, we have to explicitly call the Question constructor​
// passing MultipleChoiceQuestion as the this object, and the parameters we want to use in the Question constructor:​
Question.call(this, theQuestion, theChoices, theCorrectAnswer);
};
// inherit the methods and properties from Question​
inheritPrototype(MultipleChoiceQuestion, Question); // A Drag and Drop Question
// Create the DragDropQuestion​
function DragDropQuestion(theQuestion, theChoices, theCorrectAnswer) {
Question.call(this, theQuestion, theChoices, theCorrectAnswer);
} // inherit the methods and properties from Question​
inheritPrototype(DragDropQuestion, Question); // Overriding Methods
DragDropQuestion.prototype.displayQuestion = function () {
// Just return the question. Drag and Drop implementation detail is beyond this article​
console.log(this.question);
}; // Initialize some questions and add them to an array​
var allQuestions = [
new MultipleChoiceQuestion("Who is Prime Minister of England?", ["Obama", "Blair", "Brown", "Cameron"], 3), new MultipleChoiceQuestion("What is the Capital of Brazil?", ["São Paulo", "Rio de Janeiro", "Brasília"], 2), new DragDropQuestion("Drag the correct City to the world map.", ["Washington, DC", "Rio de Janeiro", "Stockholm"], 0)
]; // Display all the questions​
allQuestions.forEach(function (eachQuestion) {
eachQuestion.displayQuestion();
});
</script>

最新文章

  1. 分布式缓存技术redis学习系列(三)——redis高级应用(主从、事务与锁、持久化)
  2. php基础教程-输出Hello World
  3. Android Studio Reference local .aar files
  4. JavaScript高级程序设计笔记之面向对象
  5. [IR] Evaluation
  6. iOS中UIKit——UIDataDetectors(数据检测器)它将电话、邮件、网址等变为链接
  7. Linux bash常用测试判断选项
  8. Android CountDownTimer 倒计时
  9. Android 四大组件之Activity生命周期
  10. GDB调试之core文件(如何定位到Segment fault)
  11. SE 2014年4月29日
  12. socket用法
  13. Chapter 1 First Sight——32
  14. C语言作业(三)
  15. SQL 知道字段名 全表搜索此字段属于哪个表
  16. C++ Primer 笔记——关联容器
  17. C#的发展历程 -- 系列介绍
  18. synchronized的简单用法
  19. 前端框架VUE----babel
  20. 【代码笔记】iOS-JSONKit的使用

热门文章

  1. NoClassDefFoundError: org/apache/juli/logging/LogFactory
  2. MQTT 协议 Client ID 长度不能超过23个字符
  3. cocos2dx 3.0 编译工程
  4. mac下的安装神奇 brew --例子 安装 mysql
  5. eyoucms 前台 getshell 复现
  6. python json.dumps() 中文乱码问题
  7. Jenkins启动报端口被占用,解决办法FAILED ServerConnector@2a265ea9{HTTP/1.1}{0.0.0.0:8080}: java
  8. Rector模式
  9. mongodb Java(八)
  10. Spring高级话题-@Enable***注解的工作原理