程序可以运行: <script language="JavaScript"> <!-- //defining question object function Question(){ this.question = Question.arguments[0]; var n = Question.arguments.length; this.answers = new Array(n-2);
for(var i=1;i<n-1;i++) this.answers[i-1] = Question.arguments[i];// this.correctAnswer = Question.arguments[n-1];}qa = new Array(); qa[0]=new Question("Who is the first president of the United States?", "George Washington", "Abraham Lincoln", "Benjamha Franklin", "Harry Truman", 0); qa[1]=new Question("When did Columbus discover America", "1249", "1942", "1492", "1294", 2); qa[2]=new Question("HongKong has returned to china for 5 years?", "True", "False", 1); document.write(qa[0].question + " " + qa[0].answers[0] + " " + qa[0].answers[1] + " " + qa[0].answers[2] + " " + qa[0].answers[3] + " " + qa[0].correctAnswer); //--> </script>
可以运行的啊!(把两个文件放在同一目录下) test.htm<body> <script language="JavaScript"> <!-- //defining question object function Question(){ this.question = Question.arguments[0]; var n = Question.arguments.length; this.answers = new Array(n-2);
for(var i=1; i<n-1; i++) this.answers[i-1] = Question.arguments[i];//这里我改了的,加了i this.correctAnswer = Question.arguments[n-1];} //--> </script> <script language="JavaScript" src="test.js"></script> </body> ===================================== test.js qa = new Array(); qa[0]=new Question("Who is the first president of the United States?", "George Washington", "Abraham Lincoln", "Benjamha Franklin", "Harry Truman", 0); qa[1]=new Question("When did Columbus discover America", "1249", "1942", "1492", "1294", 2); qa[2]=new Question("HongKong has returned to china for 5 years?", "True", "False", 1); document.write(qa[0].question + " " + qa[0].answers[0] + " " + qa[0].answers[1] + " " + qa[0].answers[2] + " " + qa[0].answers[3] + " " + qa[0].correctAnswer);
<script language="JavaScript">
<!--
//defining question object
function Question(){
this.question = Question.arguments[0];
var n = Question.arguments.length;
this.answers = new Array(n-2);
for(var i=1;i<n-1;i++)
this.answers[i-1] = Question.arguments[i];// this.correctAnswer = Question.arguments[n-1];}qa = new Array();
qa[0]=new Question("Who is the first president of the United States?",
"George Washington",
"Abraham Lincoln",
"Benjamha Franklin",
"Harry Truman",
0);
qa[1]=new Question("When did Columbus discover America",
"1249",
"1942",
"1492",
"1294",
2);
qa[2]=new Question("HongKong has returned to china for 5 years?",
"True",
"False",
1);
document.write(qa[0].question + " " + qa[0].answers[0] + " " + qa[0].answers[1] + " " + qa[0].answers[2] + " " + qa[0].answers[3] + " " + qa[0].correctAnswer);
//-->
</script>
test.htm<body>
<script language="JavaScript">
<!--
//defining question object
function Question(){
this.question = Question.arguments[0];
var n = Question.arguments.length;
this.answers = new Array(n-2);
for(var i=1; i<n-1; i++)
this.answers[i-1] = Question.arguments[i];//这里我改了的,加了i this.correctAnswer = Question.arguments[n-1];}
//-->
</script>
<script language="JavaScript" src="test.js"></script>
</body>
=====================================
test.js
qa = new Array();
qa[0]=new Question("Who is the first president of the United States?",
"George Washington",
"Abraham Lincoln",
"Benjamha Franklin",
"Harry Truman",
0);
qa[1]=new Question("When did Columbus discover America",
"1249",
"1942",
"1492",
"1294",
2);
qa[2]=new Question("HongKong has returned to china for 5 years?",
"True",
"False",
1);
document.write(qa[0].question + " " + qa[0].answers[0] + " " + qa[0].answers[1] + " " + qa[0].answers[2] + " " + qa[0].answers[3] + " " + qa[0].correctAnswer);
原先我把Question对象的定义放在head部分就老出现对象未定义的错误,像你那样放在body中就没有错了。能说明一下原因吗?