javascript 类型错误:日期不是构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30130241/
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
TypeError: Date is not a constructor
提问by Pepper Ky
So, I've been making forms for my company for some time now with pretty easy Javascript that has worked for me in the past. However all of a sudden it's kicking out the error: TypeError: Date is not a constructor
因此,我使用过去曾对我来说有效过的非常简单的 Javascript 为我的公司制作表格已经有一段时间了。然而,突然之间它踢出了错误:TypeError: Date is not a constructor
The Code:
代码:
var Date = this.getField("Text1");
Date.value = util.printd("mm/dd/yyyy",new Date());
It works on all my old forms, but now it won't work on new ones... and I've tried making a new button on an old form - just copying and pasting the code, and then it'll break all the other buttons and spit out the same error.
它适用于我所有的旧表单,但现在它不适用于新表单......我已经尝试在旧表单上创建一个新按钮 - 只需复制和粘贴代码,然后它就会破坏所有其他按钮并吐出同样的错误。
Running: Windows 7 64-bit with Acrobat XI 11.0.10
运行:带有 Acrobat XI 11.0.10 的 Windows 7 64 位
回答by ssube
The variable Date
is hiding the global function Date
and causing this error. Because of how scoping works in JS, the inner-most use of a name is the one that matters.
该变量Date
隐藏了全局函数Date
并导致此错误。由于作用域在 JS 中的工作方式,名称的最内部使用才是最重要的。
In this case, you declare var Date
which becomes the only Date
the function knows about. When you assign it a field or text (Date = this.getField...
), you hide the global class.
在这种情况下,您声明var Date
哪个成为Date
函数唯一知道的。当您为其分配字段或文本 ( Date = this.getField...
) 时,您隐藏了全局类。
You can rename your variable (I would suggest date
, as capital names are typically reserved for types) or explicitly reference new window.Date
when you go to construct a new date.
您可以重命名变量(我建议date
,因为大写名称通常是为类型保留的)或new window.Date
在构建新日期时显式引用。
回答by garish
This worked for me:
这对我有用:
var d = new window.Date();
回答by K.L Sathish
You can't define a variable called "Date" because there's a built-in object in JS called that (you're using it in your code, actually). Change the name to something else.
你不能定义一个名为“Date”的变量,因为在 JS 中有一个内置对象叫做它(实际上你在你的代码中使用它)。将名称更改为其他名称。
var Date= somthing;<-- wrong declare, you should not use build -in object name
var 日期 = 某事;<-- 错误声明,您不应该使用内置对象名称
回答by Paulo Victor Ferreira
I was having this problem and I solved it! don't use "Date"as variable because this causes conflict with Global function Date();
我遇到了这个问题,我解决了!不要使用“Date”作为变量,因为这会导致与全局函数Date()发生冲突;
Exemple: Wrong !
例子:错了!
var Date = new Date();
document.getElementById('dateCopy').innerHTML = Date.getFullYear();
Right:
对:
var DateTime = new Date();
document.getElementById('dateCopy').innerHTML = DateTime.getFullYear();
In your case:
在你的情况下:
var DateTime = this.getField("Text1");
DateTime.value = util.printd("mm/dd/yyyy",new Date());