最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Missing '()' invoking a constructor - Stack Overflow

programmeradmin0浏览0评论

I have this code below to return the current year (I also have it in jsFiddle). jsFiddle keeps giving me an error: "Missing '()' invokes a constructor", and I don't know what it means or how to get rid of it. The code still works, but I'd like to know what the heck is causing the error:

HTML:

jQuery:

var currentYear = (new Date).getFullYear();
$(document).ready(function() {
    $("#year").text(currentYear);
});

Thanks!

I have this code below to return the current year (I also have it in jsFiddle). jsFiddle keeps giving me an error: "Missing '()' invokes a constructor", and I don't know what it means or how to get rid of it. The code still works, but I'd like to know what the heck is causing the error:

HTML:

jQuery:

var currentYear = (new Date).getFullYear();
$(document).ready(function() {
    $("#year").text(currentYear);
});

Thanks!

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 30, 2013 at 15:07 alexwc_alexwc_ 1,6032 gold badges25 silver badges35 bronze badges 4
  • you have to use new Date() – Ashok Commented Sep 30, 2013 at 15:08
  • Out of interest, where are you seeing that error message? In the console? And what browser are you using? (I don't see it in either Chrome or IE) – musefan Commented Sep 30, 2013 at 15:14
  • (didn't see this comment here!) when you click the JSHint button at the top of the page, red dots appear beside any errors that are present. Hover over the red dot to see what the error is. It's a great tool for new folks like myself! (Chrome) – alexwc_ Commented Sep 30, 2013 at 15:28
  • 1 This is a good Q&A and should not have been downvoted. – Geek Stocks Commented Dec 10, 2016 at 5:49
Add a comment  | 

3 Answers 3

Reset to default 21

It is the new Date part that is causing the warning. This will fix it:

var currentYear = (new Date()).getFullYear();

You are missing the brackets for Date, it should be Date()

So:

var currentYear = (new Date()).getFullYear();

Missing a parentheses

new Date()
发布评论

评论列表(0)

  1. 暂无评论