I recently came across some code that was using syntax that I wasn't too familiar with. At first, it appeared to be a function that was part of an object, but I can't seem to access it. I've found information here on it, but I'm still not sure why someone would use this over a standard function signature. This is an example of the code, and what I'm trying to do with it.
const secretFunction = Symbol('secretFunction');
class SecretController {
constructor(){
...
}
[secretFunction](){
console.log("I AM THE SECRET!");
}
}
So, what I am trying to do is test this code with a tool called jest. I'm doing so like:
let secretController = new SecretController();
let secretFunction = Symbol('secretFunction');
describe("TEST", function(){
it('What is this?', function(){
secretController[secretFunction](); // <-- doesn't work
})
})
I've tried this a few other ways, but I get the same type of result. That is:
TypeError: secretController[secretFunction] is not a function
What is the purpose of writing a function this way? How do you use functions like this? How do you test them?