为什么当我运行这个时有未定义
why there is undefined when i run this
<html>
<head>
<title>js_trial</title>
<script language="javascript">
now = new Date();
document.write("today is: "+now+"<br/>");
function print (number){
if(number >= 4);
return document.write("the number "+number);
}
var num = 4;
var doub = print(num);
document.write(doub);
</script>
</head>
<body>
</html>
输出:今天是: 星期三 二月 15 2012 23:31:19 GMT+0800 (台北标准时间(数字 4未定义
为什么 4 后有未定义?
document.write
的返回值始终为 undefined
。
您document.write
print
的返回值,该值具有return document.write(...)
。
你的 if 语句不起作用:
now = new Date();
document.write("today is: "+now+"<br/>");
function print (number){
if(number >= 4) {
document.write("the number "+number);
}
}
var num = 4;
print(num);
而且document.write
没有回应.因此,"未定义"。
当你打电话给document.write(doub);
时,你正在做的事情本质上是doucment.write(document.write("the number "+number));
。
undefined
是内部document.write
调用的返回值,外部 document.write 调用告诉您没有值。
函数 document.write 总是返回 undefined!你要做的是:将"数字4"写入文档(document.write("the number "+number);
(,并将写入函数的返回值(未定义(存储到变量doub
之后,你doub
写入你的文档,这会导致附加的"undefined"。
你想写而不是你的代码,是:
function print (number){
if(number >= 4){
return ("the number "+number);
}
}
var num = 4;
var doub = print(num);
document.write(doub);
您的函数 print 现在返回一个字符串"数字 4",该字符串存储在 doub
中,最后写入文档。
或
function print (number){
if(number >= 4){
document.write("the number "+number);
}
}
var num = 4;
print(num);
调用函数 print,它将"数字 4"直接写入文档。
编辑:
<script language="...">
已过时,请改用<script type="text/javascript">
!
您的 if 条件不起作用,请使用
if (condition) {
//your code here
}
相关文章:
- 谷歌地图:函数返回未定义的值在console.log中运行良好
- 变量在运行时未定义,但在使用调试器时定义
- Javascript - 加载函数运行时未定义的变量
- Microsoft JScript运行时错误:无法获取属性'的值;样式':对象为null或未定义
- Microsoft JScript运行时错误:'Sys'未定义(在mvc3 c#剃刀中)
- Javascript'元素'在ie中未定义,但在chrome和firefox中运行良好
- 运行此程序后出现错误,未捕获的类型错误:无法读取未定义的属性“推送”
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- Meteor with Parse - 在 Android 设备上运行 - 未定义 Parse
- 未定义再生器运行时
- Javascript 运行时错误:“应用程序未定义”
- 什么更快?运行空函数或检查函数是否未定义
- Javascript值未定义,函数在赋值后运行
- JavaScript运行时错误,未定义jQuery
- 对象在循环中运行时未定义,但在按顺序执行时未定义
- 脚本似乎在加载jQuery之前运行-未定义Uncaught ReferenceError:$
- 如何解决Microsoft JScript运行时错误:'$'未定义
- Traceur运行时:超级表达式必须为null或函数,而不是未定义的
- 0x800a138f-JavaScript运行时错误:无法获取属性'fn'的未定义引用或null引用
- IE7 javascript错误:未定义运行时错误microsoft jscript:dropdownlist