javascript 错误 { [本机代码] }
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8856125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript error { [native code] }
提问by ristenk1
Hi I'm trying to do some basic javascript and am getting "native code" instead of what I want:
嗨,我正在尝试做一些基本的 javascript 并且得到“本机代码”而不是我想要的:
<script type="text/javascript">
var today = new Date();
document.write(today + "<br />");
//document.write(today.length + "<br />"); - was getting "undefined"
//document.write(today[0] + "<br />"); - was getting "undefined"
document.write(today.getMonth + "<br />");
document.write(today.getMonth + "<br />");
document.write(today.getFullYear + "<br />");
</script>
output was:
输出是:
Fri Jan 13 14:13:01 EST 2012
function getMonth() { [native code] }
function getDay() { [native code] }
function getFullYear() { [native code] }
what I want is to get the current Month, Day, Year and put it into an array variable that I will be able to call later on. I'm not getting far because of this native code. Can someone tell me what it is and hopefully, more importantly I can complete this project? Thank you for your time and help, it is much appreciated!
我想要的是获取当前的月、日、年并将其放入一个数组变量中,以便稍后调用。由于这个本机代码,我没有走远。有人能告诉我它是什么吗,更重要的是我能完成这个项目吗?感谢您的时间和帮助,非常感谢!
回答by cambraca
The getMonth
and the rest are functions, not properties, when you call just today.getMonth
you are getting a reference to the actual function. But, if you execute it using parenthesis, you get the actual result.
在getMonth
剩下的都是函数,而不是属性,当你调用只是today.getMonth
你得到的实际函数的引用。但是,如果使用括号执行它,则会得到实际结果。
Your code should be:
你的代码应该是:
document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
回答by ShankarSangoli
You are missing the parenthesis()
.
您缺少括号()
。
document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
回答by FishBasketGordo
getMonth
and getFullYear
are functions, so you need to invoke them. Note the parentheses:
getMonth
和getFullYear
是函数,所以你需要调用它们。注意括号:
document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
As you have it, it's printing the string representations of the functions, not the functions' values.
正如您所拥有的,它打印的是函数的字符串表示,而不是函数的值。
回答by qwertymk
document.write(today.getMonth() + "<br />"); // notice the ()'s to invoke the function
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");