The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> functionName is not a function
CharliePrince
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharliePrince
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 24 2020, 09:06 AM
Post #3


.
********

Group: WDG Moderators
Posts: 8,798
Joined: 10-August 06
Member No.: 7



There's no method called "getDatum", AFAIK. Maybe you're thinking of "getDate()"?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Dec 24 2020, 11:09 AM
Post #4


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 19,720
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! Attached Image
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 24 2020, 01:43 PM
Post #5


Programming Fanatic
********

Group: Members
Posts: 1,816
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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Dec 24 2020, 02:21 PM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 19,720
Joined: 9-August 06
Member No.: 6



Ah! I, obviously, didn't know that. Thanks for the heads up! Attached Image
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th June 2021 - 10:38 PM