functionName is not a function |
functionName is not a function |
CharliePrince |
Dec 23 2020, 07:48 PM
Post
#1
|
Novice Group: Members Posts: 24 Joined: 7-November 20 From: Saint Louis, MO Member No.: 27,623 |
Can anyone tell me how to deal with the error from the code below?
TypeError: this.getDatum is not a function this.getDatum() runs error-free when call directly (see first call to processThis) this.getDatum() errors when called on a delay using setTimeout() CODE class thisClass { constructor() { this.getDatum = function() { return 777; } } processThis() { let value = this.getDatum(); // (TypeError: this.getDatum is not a function) when called via setTimeout()?? console.log(`${value} processed error free`); } } let myObj = new thisClass(); myObj.processThis(); // processes error-free setTimeout(myObj.processThis, 1000); // causes (not a function) error in thisClass.processThis setTimeout(myObj.processThis, 2000); // causes (not a function) error in thisClass.processThis |
CharliePrince |
Dec 23 2020, 09:34 PM
Post
#2
|
Novice Group: Members Posts: 24 Joined: 7-November 20 From: Saint Louis, MO Member No.: 27,623 |
I (think) this is what might be happening . . .
after the 1000 ms passes CODE setTimeout(myObj.processThis, 1000); processThis is called. And by that time myObj, as well as thisClass have gone out of scope or something. Regardless, I've found a way around this issue by just not using a js class This post has been edited by CharliePrince: Dec 23 2020, 09:34 PM |
Christian J |
Dec 24 2020, 09:06 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,653 Joined: 10-August 06 Member No.: 7 |
There's no method called "getDatum", AFAIK. Maybe you're thinking of "getDate()"?
|
pandy |
Dec 24 2020, 11:09 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Just to nitpick, I'd change the grave accents/back-ticks here to quotes.
CODE console.log(`${value} processed error free`); And a Merry Christmas to you! |
CharlesEF |
Dec 24 2020, 01:43 PM
Post
#5
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
The backticks were added to Javascript, I think its called 'Tagged Template' in ES6. See this page.
|
pandy |
Dec 24 2020, 02:21 PM
Post
#6
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Ah! I, obviously, didn't know that. Thanks for the heads up!
|
Lo-Fi Version | Time is now: 19th April 2024 - 10:06 PM |