Javascript对象未定义关键字在输出语句结束
Javascript object undefined keyword in the end of output statement
我在javascript中制作了一个对象,它运行良好,但在每个语句的末尾都说"未定义"。这是我的javascript。
<script>
window.onload=function(){
function sports(sportsname, country, player, difficulty){
this.sportsname = sportsname;
this.country = country;
this.player = player;
this.difficulty = difficulty;
this.hissportstaste = sportstaste;
}
var Cricket = new sports("Cricket", "Pakistan", "Shahid Afridi", "Difficult");
var Football = new sports("Football", "Spain", "David Villa", "Difficult");
var Tennis = new sports("Tennis", "Switzerland", "Roger Federer", "Difficult");
function sportstaste(){
if(this.country == "Pakistan"){
document.write(this.player + " Is the best and he is a " + this.sportsname + " Player");
}
if(this.country == "Spain"){
document.write(this.player + " Is the best and he is a " + this.sportsname + " Player");
}
if(this.country == "Switzerland"){
document.write(this.player + " Is the best and he is a " + this.sportsname + " Player");
}
}
document.write(Cricket.hissportstaste());
document.write("<br>");
document.write(Football.hissportstaste());
document.write("<br>");
document.write(Tennis.hissportstaste());
document.write("<br>");
}
</script>
它给出的输出是。
Shahid Afridi Is the best and he is a Cricket Playerundefined
David Villa Is the best and he is a Football Playerundefined
Roger Federer Is the best and he is a Tennis Playerundefined
我期待这个输出。
Shahid Afridi Is the best and he is a Cricket Player
David Villa Is the best and he is a Football Player
Roger Federer Is the best and he is a Tennis Player
任何想法?
变化
document.write(Football.hissportstaste());
Football.hissportstaste();
和类似的调用相同:您的函数不返回任何内容(undefined
)并且已经写入文档。
但是你应该尽量避免document.write
,这个函数有非常特殊的用途(它不能在文档加载后使用),你应该学会改变DOM(参见这个例子)。
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 在JavaScript中输出转义字符
- 如何在jQuery中将函数的输出分配给变量
- Javascript OOP-为什么这两个几乎完全相同的语句给出不同的输出
- for语句循环小写输出
- 在 HTML 中循环写入(输出)语句
- JavaScript - 为什么在使用语句之前声明的变量时与 if/else 语句的输出不同
- 与 for 的结果混淆..在语句 JS 输出中
- 使用Switch语句的JavaScript-没有得到预期的输出
- 输出在 JavaScript 中不带语句
- Javascript if/else语句不返回任何输出
- 通过if else语句显示特定的输出
- Meteor Template不会在每个语句中输出数据
- “甚至Steven"输出奇数的While语句取代偶数
- 返回错误输出的Javascript条件语句
- 淡出使用if语句输入和输出
- 复杂的if语句运行代码而不考虑条件输出
- 如果..否则如果..语句在 JavaScript 中没有给出任何输出
- Javascript对象未定义关键字在输出语句结束