alexp95
alexp95•2w ago

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
Leokuma
Leokuma•2w ago
Maybe myFunction.toString()
raunioroo
raunioroo•2w ago
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?
NDH
NDH•2w ago
If you need a class that will save constantly updating functions
class Funcy {
funcs: Map<string, any[]> = new Map()

// saves any change to a function
// in a named array
save(name: string, func: any) {
if (this.funcs.has(name)) {
const fList = this.funcs.get(name);
if (fList) fList.push(func); // add function
} else {
this.funcs.set(name, [func]) // first one
}
}
// retuen an array of all changes to
// a named function
getAll(name) {
return this.funcs.get(name)
}
}

const funcy = new Funcy();
let myFunc1 = (e: string) => console.log(e);
funcy.save("myFunc", myFunc1)
let myFunc2 = (a: number) => console.log(a);
funcy.save("myFunc", myFunc2)

const myFuncs = funcy.getAll("myFunc");
console.log(myFuncs![1].toString())
myFuncs![0]("Yup!")
class Funcy {
funcs: Map<string, any[]> = new Map()

// saves any change to a function
// in a named array
save(name: string, func: any) {
if (this.funcs.has(name)) {
const fList = this.funcs.get(name);
if (fList) fList.push(func); // add function
} else {
this.funcs.set(name, [func]) // first one
}
}
// retuen an array of all changes to
// a named function
getAll(name) {
return this.funcs.get(name)
}
}

const funcy = new Funcy();
let myFunc1 = (e: string) => console.log(e);
funcy.save("myFunc", myFunc1)
let myFunc2 = (a: number) => console.log(a);
funcy.save("myFunc", myFunc2)

const myFuncs = funcy.getAll("myFunc");
console.log(myFuncs![1].toString())
myFuncs![0]("Yup!")
©TriMoon™
©TriMoon™•2w ago
Maybe @alexp95 means getters? 🤔
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
*getSides() {
yield this.height;
yield this.width;
yield this.height;
yield this.width;
}
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100
console.log([...square.getSides()]); // [10, 10, 10, 10]
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
*getSides() {
yield this.height;
yield this.width;
yield this.height;
yield this.width;
}
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100
console.log([...square.getSides()]); // [10, 10, 10, 10]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#methods
MDN 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.
©TriMoon™
©TriMoon™•2w ago
const obj = {
log: ["a", "b", "c"],
get latest() {
return this.log[this.log.length - 1];
},
};

console.log(obj.latest);
// Expected output: "c"
const obj = {
log: ["a", "b", "c"],
get latest() {
return this.log[this.log.length - 1];
},
};

console.log(obj.latest);
// Expected output: "c"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
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.
alexp95
alexp95OP•2w ago
@©TriMoon™ I mean how do you run a function that is in a string?
©TriMoon™
©TriMoon™•2w ago
For that you do the same as any other code inside a string: You eval it...
alexp95
alexp95OP•2w ago
Okay, thank you
©TriMoon™
©TriMoon™•2w ago
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...

Did you find this page helpful?