4 ms·
Interesting project. I see in some classes you set the object "self" to "this" and use it later in anonymous functions. That seems to be a common pattern in a
by beernutz 12y ago
Interesting project.
I see in some classes you set the object "self" to "this" and use it later in anonymous functions. That seems to be a common pattern in a lot of peoples code.
It seems like naming the "self" object something more descriptive and specific would make sense in general, but I can't help wondering if there is a more fundamental reason for using "self".
Is this common in OO parlance for a reason?
- mdaniel 12y agoThat is a common JS idiom because the binding of "this" changes inside function calls, so one must capture the outer binding of "this" which when used in an object scenario does behave like the same keyword used in traditional OO. The naming of "self" could just be a synonym or a nod to Python (I'd have to do research to know definitively)
- beernutz 12y agoThat makes sense. I wonder though if there is a better convention than "self". That by itself (to me at least) does not say much about which "self" we are describing. Maybe it is just a matter of personal preference, but I like to be more specific and descriptive. It reminds me of the practice of using "throw-away" variables in for loops. Personally I would still rather name the variable "loop" instead of "i".
- mdaniel 12y agoOk, you are free to propose an alternative - either here or to your team - but be sure you and your team are prepared to have to adjust if and when you join another JS team. And if I had to name loop variables "loop" I would lose my mind. But I'm glad it works for you and your team, so long as you never have nested for loops.