My question is: Why is the parent undefined?
TypeError: undefined is not an object (evaluating 'layer._element.appendChild')
Or: How can I use the value of the variable "name" instead of "testSwitch"?
The parent attribute expects a layer instance (testSwitch, in your example) not a String value ("testswitch"). What you're passing here is just a String, not a reference to another layer.myLayer.name will always return a String, so you can't use a layer's name attribute to set parents. I may need to know what you're trying to do here to give you a better solution, but in this case the parent should just be testSwitch.
I like to use createSwitch() more than once and control the name of testSwitch by the parameter I pass to the function...
You're thinking about this wrong... here's how I would approach it.
What things are called inside that function doesn't really matter.
Have your function return the testSwitch layer and name it before you call function.
testSwitch = createSwitch()
Inside your function
createSwitch = -> foo = new Layer name: "whatever"
FooBg = new Layer parent: foo
That should essentially do what you want.
Name is just a nickname not actual variable name.
Also that! I had that all typed out an I'm pretty sure my son jumped on me and I put the phone down and played legos instead. 😅