TypeError: Date is not a constructor

前端 未结 4 903
逝去的感伤
逝去的感伤 2021-01-17 16:05

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 er

4条回答
  •  不要未来只要你来
    2021-01-17 16:51

    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.

    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.

    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.

提交回复
热议问题