I have built a javascript class that does things to the dom. To simplify the use from outside as much as possible I now have methods like these:
hello.add(id);
hello.delete(id);
hello.select(id);
To make the above work, I have setup a static class that uses the non static class like this:
Static class
class hello {
static add(id) {
let hello = new HelloCore();
hello.add(id);
}
static delete(id) {
let hello = new HelloCore();
hello.delete(id);
}
static select(id) {
let hello = new HelloCore();
hello.select(id);
}
}
Non static class
The real code is 500 lines with a constructor and so on.
class helloCore() {
add() {
// Do something
}
delete() {
// Do something
}
select() {
// Do something
}
}
It's now super simple but I wonder if there is a performance issue with this approach. My guess is that it store a new javascript object on every static call into memory?
- Do I need to worry about it?
- If yes, what would be a better approach, but still super simple?
What I want to stay away from
From the start I had always needed to pass the object around to the functions where it's going to be used. See below.
let hello = new HelloCore();
function my_function(hello) {
let id = 2;
hello.add(id);
}
my_function(hello);
With the static approach I don't need to pass the object around everywhere. I can just trigger the function from anywhere.
I could use a global var hello = new Hello();
but then I need to make sure it's loaded first, that I don't forget about it and it means extra code for the user.