I wonder what the memory performance is like compared to this:
constprivates=newWeakMap()classAnimal{constructor(name,job){privates.set(this,{name,job})this.aPublicOne=true}// Real Gettersgetname(){returnprivates.get(this).name}getjob(){returnprivates.get(this).job}// Real Setterssetname(name){privates.get(this).name=name}setjob(job){privates.get(this).job=job}}
Liek, I know instantiating closures isn't the same as actually declaring separate different functions. But it also is more expensive than object methods, to my great disdain.
Will this be somewhere in the middle, or actually worse than both?
I'm not sure myself, honestly. I'd love to know. When I have the time, I'll try to create a rigorous experiment for this using the DevTools Memory tools.
If I were to make an educated guess, I'd say the difference in memory efficiency would be negligible.
const privates = new WeakMap()
class Animal {
constructor(name, job) {
privates.set(this, { name, job })
console.log('I'm a total GENIUS!')
}
}
// A few lines later on the code ....
function privates() {
console.log("No, you are not. Do you remember the NO-GLOBALS rules?")
}
1. Uncaught SyntaxError: missing ) after argument list
You would have caught this one if you used highlighting:
constprivates=newWeakMap()classAnimal{constructor(name,job){privates.set(this,{name,job})console.log('I'matotalGENIUS!')
}
}
// A few lines later on the code ....
function privates() {
console.log("No, you are not. Do you remember the NO-GLOBALS rules?")
}
vs
constprivates=newWeakMap()classAnimal{constructor(name,job){privates.set(this,{name,job})console.log("I'm a total GENIUS!")}}// A few lines later on the code ....functionprivates(){console.log("No, you are not. Do you remember the NO-GLOBALS rules?")}
2. Uncaught SyntaxError: Identifier 'privates' has already been declared (pointing at the function, not the const)
Have you forgotten declaration collision? (TypeScript would also have caught it statically)
3. "globals" are fine when you are thinking in modules
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I wonder what the memory performance is like compared to this:
Liek, I know instantiating closures isn't the same as actually declaring separate different functions. But it also is more expensive than object methods, to my great disdain.
Will this be somewhere in the middle, or actually worse than both?
I'm not sure myself, honestly. I'd love to know. When I have the time, I'll try to create a rigorous experiment for this using the DevTools Memory tools.
If I were to make an educated guess, I'd say the difference in memory efficiency would be negligible.
How funny is it, though, that this method is still using closures, the 5 functions capturing
privates
:DIt makes you realize that even though closures are so ubiquitous in JavaScript, not everyone truly understands it.
1.
Uncaught SyntaxError: missing ) after argument list
You would have caught this one if you used highlighting:
vs
2.
Uncaught SyntaxError: Identifier 'privates' has already been declared
(pointing at the function, not theconst
)Have you forgotten declaration collision? (TypeScript would also have caught it statically)
3. "globals" are fine when you are thinking in modules