[Cosmo-dev] Re: [commits-cosmo] (br) [4567] this is a better way not to clobber namespaces.

Matthew Eernisse mde at osafoundation.org
Fri Jun 8 12:04:08 PDT 2007


Jeremy,

Actually not a bad aside at all. I'd rather have the question asked so 
we can be sure it's not going to bite us in the ass at some point later.

I'm not sure how or why I started using 'self.' Either I saw someone 
else doing it and just picked it up, or did it because I use a lot of 
Ruby. I can't remember which. :)

I'm open to the idea of changing to use something like '_this' -- but it 
would be some amount of work to convert all our code to a new 
convention, so I guess the questions are:

1. How harmful is using 'self' for other consumers of our code?

Since it's always defined at the module- or method-level with "var self 
= this;" I'm guessing the effects would be either insignificant or nil.

2. How likely is it that it will cause problems for us in our code?

I've never used 'self' to refer to the current window (obviously, since 
I was oblivious to the fact that it's a reserved keyword :)) -- I've 
always just used 'window.' Is there some advantage you get from using 
'self' instead of 'window'? Is it likely that we'd ever want to do that?

3. How much work would it be to convert?

Probably not that much. We would have to do a global replace like so:

"var self = this;" => "var _this = this;"
"self." => "_this."

Doesn't seem that terrible -- but we likely want to do it at the 
beginning of a release cycle to reduce the QA impact. It it's going to 
break a few things here and there, it's probably better to do it at a 
time when lots of other stuff is expected to be broken.

Anyone have any thoughts?


Matthew


Jeremy Epstein wrote:
> Matthew and co.
> 
> by the way, this may seem like an awful aside, but where did the use of 
> "self" as a proxy for "this" come in?
> I say this because nominally in javascript "self" is a reserved word 
> that refers to the current window when used in event handlers. I'm not 
> disputing why you might want to use a variable like that, only the 
> choice of name.
> 
> Jeremy



More information about the cosmo-dev mailing list