Function as a value?
I would like to know what is the best way to store a function as a value in a class because I am trying to track a constantly updating function
9 Replies
Maybe
myFunction.toString()
Maybe im missing something from the question, but.. Functions in javascript are just values like string, number, or any other value. So, store it as is in a class member, just like you would store a string or any other type?
If you need a class that will save constantly updating functions
Maybe @alexp95 means
getters
? 🤔
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#methodsMDN Web Docs
Classes - JavaScript | MDN
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are unique to classes.
MDN Web Docs
get - JavaScript | MDN
The get syntax binds an object property to a function that will be called when that property is looked up. It can also be used in classes.
@©TriMoon™ I mean how do you run a function that is in a string?
For that you do the same as any other code inside a string: You
eval
it...Okay, thank you
PS:
eval
ing any code is not the solution to any coding problem, maybe you should rethink your actions...
Most sites and browsers do not allow eval
using CSP...