If I have to choose, I prefer to use 'that' or something more
descriptive like 'originalThis'. But the best solution (IMHO) is to
write a bind function in Function.prototype that returns another
function with a fixed value for 'this'. For example:

Function.prototype.bind = function(obj) {
        var that = this;
        return function() { return that.apply(obj, arguments); };
};

//Now it is shorter and more elegant
var Dialog = function() {
 this.width = 400;
 this.setWidth = (function(newWidth) {
   this.width = newWidth;
 }).bind(this);
}

Also, you can use another common patterns to construct your original
object that will encapsulate 'width'.

--
S.Cinos
JavaScript Developer at Tuenti.com



2011/3/3 Jarek Foksa <ja...@kiwi-themes.com>:
> 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/jsmentors@jsmentors.com/
>
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/jsmentors@googlegroups.com/
>
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to