Hi, > 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 ?
`this` is never scope in Javascript (unless you use `with`, which simulates scope to an extent). It's just an object reference. If you want to refer to a property on the instance, you have to be explicit (either by typing "this." or using "with"). So the answer to your question is no, there's no reasonable way (that I know of) to define a member function such that unqualified symbols resolve to properties on `this`, other than to wrap the code of every member function inside a `with` statement, which I wouldn't recommend for several reasons. (I say "reasonable" because it's probably possible using `eval` and functions generated on the fly.) Your best bet, in my book, is to simply use `this.` when referring to a property on `this`. I think it's clearer anyway (in fact, I always use `this.` in Java and C#, even though the compiler doesn't need me to -- in my view, *I* need me to, as do any code maintainers coming after me.) If the length bothers you, you can put `var t = this;` at the top of every function and use `t.` instead of `this.`. (Doing that also has the advantage that the meaning to `t.` will survive transition into closures, whereas `this.` will not.) HTH, -- T.J. Crowder Independent Software Consultant tj / crowder software / com www.crowdersoftware.com On Apr 30, 1:30 pm, Ran Berenfeld <berenfeld...@gmail.com> wrote: > 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 <t...@crowdersoftware.com>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 <berenfeld...@gmail.com> 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 > > prototype-scriptacul...@googlegroups.com. > > > To unsubscribe from this group, send email to > > prototype-scriptaculous+unsubscr...@googlegroups.com<prototype-scriptaculou > > s%2bunsubscr...@googlegroups.com> > > . > > > 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 > > prototype-scriptacul...@googlegroups.com. > > To unsubscribe from this group, send email to > > prototype-scriptaculous+unsubscr...@googlegroups.com<prototype-scriptaculou > > s%2bunsubscr...@googlegroups.com> > > . > > 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 prototype-scriptacul...@googlegroups.com. > To unsubscribe from this group, send email to > prototype-scriptaculous+unsubscr...@googlegroups.com. > 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 prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.