js報錯:TypeError: Date is not a constructor
前言
半夜敲程式碼總是容易出bug,就在剛剛又出了個小bug,把我整蒙了。
我在寫一個時間格式化的函式,需要先new Date()
let date = new Date(timeStamp);
單純看上面的程式碼是沒有問題的,但執行的時候就報錯
我尋思著,這必不可能阿!!我還各種嘗試都還是報錯。
後來我看著這段程式碼,感覺哪裡不太對勁
我記得平時的時候Date是綠色的,像下面這樣
但是現在卻是這樣的↓
這時可能已經有人發現了,原來是我下面宣告瞭一個Date,然後在這個作用域裡,
const Date會有變數提升,所以我上面new Date的時候,這個Date是我自己宣告的Date,而不是js中的Date,然後就報錯說Date不是一個建構函式。
解決方法
把 const Date = date.getDate()
改成 const Day = date.getDate()
總結
在用到時間模組Date的時候,記得不要自己宣告一個變數名為Date,要不然痛苦面具
相關文章
- VUE前端打包報錯:TypeError: Class extends value undefined is not a constructor or nullVue前端ErrorUndefinedStructNull
- TypeError: SizeOnlySource is not a constructorErrorStruct
- js 函式報錯“Uncaught: TypeError xxx is not a function”JS函式ErrorFunction
- Ajax 報錯jquery-3.3.1.min.js:2 Uncaught TypeError: Cannot use 'in' operator to seajQueryJSError
- 1.python報錯:TypeError: 'int' object is not subscriptablePythonErrorObject
- TypeError: The ‘compilation‘ argument must be an instance of Compilation 報錯Error
- TypeError:a.replaceisnotafunction錯誤ErrorFunction
- Python報錯:TypeError: a bytes-like object is required, not ‘str‘PythonErrorObjectUI
- vuex報錯 vuex__WEBPACK_IMPORTED_MODULE_1__.default.store is not a constructorVueWebImportStruct
- JS DateJS
- python報錯:TypeError: slice indices must be integers or None or have an __index__ methodPythonErrorNoneIndex
- el-tree 報錯 TypeError: Cannot read property ‘setCheckedKeys‘ of undefined“ErrorUndefined
- jsp報錯JS
- Pycharm:設定完Anaconda後報錯TypeError: an integer is required (got type bytes)PyCharmErrorUIGo
- react 和redux整合報TypeError: Object(...) is not a functionReactReduxErrorObjectFunction
- TypeError: Object of type ‘datetime‘ is not JSON serializableErrorObjectJSON
- 7.107 JSON Type ConstructorJSONStruct
- js本地物件——Date()JS物件
- myeclipse去掉js報錯EclipseJS
- Extjs報錯處理JS
- npm報錯 TypeError [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string.Received undefineNPMError
- 深入分析js中的constructor 和prototypeJSStruct
- flutter升級後 執行專案報錯 Warning: Podfile is out of dateFlutter
- 特殊字元、Date、JS應用字元JS
- js學習三-日期DateJS
- myeclipse js檔案報錯EclipseJS
- fastjson 返回json字串,JSON.parse 報錯ASTJSON字串
- js new Date怪異行為JS
- js Date.now()函式用法JS函式
- 小陳學JS js內建物件 Date物件JS物件
- js基礎–Date.parse()與Date.getTime()方法詳解JS
- 用element-ui 填充date-picker時,報value.getTime is not a function錯誤UIFunction
- Prototype/ConstructorStruct
- JavaScript constructorJavaScriptStruct
- Default copy constructor does not call correct base(轉) constructorStruct
- js物件陣列Date的比較JS物件陣列
- BUG: JS 修改Date的TimezoneJS
- javascript得知 - js的Date物件函式JavaScriptJS物件函式