Thanks a lot for your reply T.J I was partially familiar with this mechanism, and now I know more :) anyway, let me re-phrase my question we all know that an event received on a DOM element (i.e button onclick event for example) will have the "this" equals to the DOM element
my question is simple : isn't there a way do define the class member functions in such a way that the scope (this) will refer to the object instance ? Thanks Ran B On Fri, Apr 30, 2010 at 9:31 AM, T.J. Crowder <[email protected]>wrote: > Hi, > > > is there a way that I can get rid of all these usages of "this" > > No. `this` is different in Javascript than in some other languages > with the same keyword, in several ways. One of these is that > unqualified references like your _div (without anything in front of > it) are not automatically resolved against property names on `this`; > all identifier resolution in Javascript is lexically-based, using the > concept of a "scope chain" (more below if you're interested). So you > have to explicitly type `this.` when accessing the properties on > `this` (just as you have to type `foo.` to access the properties on > `foo`), or as you've found, you can use `with`. Be aware that using > `with` has some "gotchas" that are not obvious. Douglas Crockford > lists many of these in his "with Statement Considered Harmful"[1] > article (I'm not a fan of "Considered Harmful" articles[2], but > Crockford lays out the issues fairly well). My point isn't "don't use > it", it's "be sure you understand the gotchas when using it". > > About the scope chain and identifier resolution: One of the things I > love about Javascript is that basically *everything* is an object, > even if some of the objects are hidden behind the scenes. One of these > behind-the-scenes objects is the "variable object" (the "binding > object of the variable environment" in the latest spec -- yikes). When > you declare a variable using `var`, you're adding a *property* to the > variable object for the current scope. The variable object has > properties for all `var`s, all functions declared in the scope, and > (if the scope relates to a function) all named arguments to the > function. When the step-by-step code within the scope is executing, > that scope's variable object is at the beginning of the "scope chain." > All unqualified references are first checked against that variable > object to see if they match a property on it. If they do, that > property is used; if not, the next variable object in the chain is > checked; etc. > > Example: > > var x = 1; > > function foo() { > var y = 2; > > function bar() { > var z = 3; > > alert(x); > alert(y); > alert(z); > } > } > > We have three "scopes" there: Global scope, `foo`'s scope, and `bar`'s > scope. Consequently, we have three variable objects. When code within > `bar` is being executed, the scope chain consists of `bar`'s variable > object, followed by `foo`'s, followed by the global variable object. > When `bar` references `z`, the `z` property is found on the first > variable object and used. When `bar` references `y`, it's not on the > first variable object but it is on the second one, so it's used from > there. Accessing `x` involves going back two variable objects to the > global one. > > As you can see, `this` doesn't come into it -- it's nowhere on the > scope chain unless you insert it, which is exactly what `with` does: > Puts the object you specify at the beginning of the scope chain. > > Now, I called the variable object a behind-the-scenes object. That's > usually true, but there's an exception: The variable object for the > global scope is the global object, which on browsers is `window` (more > accurately, on browsers the global object has a property, `window`, it > points to itself with). That's why when you declare variables and > functions at global scope, they magically show up as properties on > `window`. > > [1] http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/ > [2] http://meyerweb.com/eric/comment/chech.html > > HTH, > -- > T.J. Crowder > Independent Software Consultant > tj / crowder software / com > www.crowdersoftware.com > > On Apr 29, 9:17 am, Ran Berenfeld <[email protected]> wrote: > > Hi all > > I have written a class using prototype Class object > > and after playing it for a while, I saw that I need to use the "with" > > keyword in the class members, > > so I can access its members > > > > here is the relevant part of the class : (the member is "_div") > > > > var JSClass = Class.create ({ > > initialize: function(initParams) { > > > > if (initParams.div) > > { > > *this*._div = $(initParams.div); > > } > > else > > { > > *this*._div = new Element(div); > > } > > }, > > // an example of a member function > > setComment: function(msg) > > { > > *with(this)* > > * {* > > * *_div.innerHTML = msg; > > * }* > > } > > > > }); > > > > is there a way that I can get rid of all these usages of "this" > > > > Thanks > > Ran > > > > -- > > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > > To post to this group, send email to > [email protected]. > > To unsubscribe from this group, send email to > [email protected]<prototype-scriptaculous%[email protected]> > . > > For more options, visit this group athttp:// > groups.google.com/group/prototype-scriptaculous?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > To post to this group, send email to > [email protected]. > To unsubscribe from this group, send email to > [email protected]<prototype-scriptaculous%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/prototype-scriptaculous?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
