Personally I like to use the same name for the object that I would typically
use in code that *uses* the object. For example I might use a variable
called 'dialog' for one of your Dialog objects, both in the constructor and
elsewhere.
The other rule I follow strictly is that if I create a variable for 'this',
then I do not use 'this' *at all* other than that one assignment. It's too
confusing to mix and match.
So, I'd write that code like so:
var Dialog = function() {
var dialog = this;
dialog.width = 400;
dialog.setWidth = function( width ) {
dialog.width = width;
}
}
-Mike
On Thu, Mar 3, 2011 at 11:08 AM, Jarek Foksa <[email protected]> wrote:
> Hi,
>
> When writing constructor functions it's very convenient to create "var
> self = this" assignment so that we could easily access other
> properties and methods even if default context has changed, for
> example:
>
> var Dialog = function() {
> var self = this;
> this.width = 400;
> this.setWidth = function(newWidth) {
> self.width = newWidth;
> }
> }
>
> But is it really a good idea to use "self" variable name for this
> purpose? It seems to be already assigned to window object by default,
> so I guess redefining it might break some third-party code.
>
> What other names would you recommend? Is there some other convention?
>
>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]