This is a shining example of what makes this community so awesome. 2010/12/3 Maxim Lacrima <[email protected]>
> Hi! > Thank you. > > 2010/12/3 Sean McArthur <[email protected]>: > > More info on how `this` > > works: > http://seanmonstar.com/post/707068021/4-ways-functions-mess-with-this > > > > > > On Fri, Dec 3, 2010 at 4:51 AM, Maxim Lacrima <[email protected]> > > wrote: > >> > >> Hi Barry! > >> > >> Thank you very much! Very informative. > >> > >> On 3 December 2010 14:31, Barry van Oudtshoorn < > [email protected]> > >> wrote: > >> > Hi Maxim, > >> > > >> > 1. The element will actually be shared by *all* the instances of that > >> > class. > >> > Take a look at http://jsfiddle.net/948G8/ -- you'll notice that the > >> > element > >> > which is injected into the document has the text 'beta'. What you > >> > probably > >> > want to do is something like this: > >> > > >> > var FooComponent = new Class({ > >> > elem: null, > >> > initialize: function() { > >> > this.elem = new Element('div'); > >> > } > >> > }) > >> > > >> > This will mean that every time you instantiate your class, you get a > >> > brand-spanking-new element for that instance. (The initialize method > is > >> > special -- MooTools automatically calls it for you every time you > >> > instantiate a class.) > >> > > >> > 2. Well, in your example, you're actually calling the function > >> > getCurrentStatus when you're setting up the class. Take a look at > >> > http://jsfiddle.net/t6jk9/ -- you'll see that f1 is called, but f2 > >> > isn't. > >> > Now, in a class, when you call a function, "this" can refer to a > couple > >> > of > >> > things. Take a look at http://jsfiddle.net/5ncg3/0/ -- you'll see > that > >> > when > >> > you call the function "checkThisOut", "this" is dependent on how > you're > >> > making that call. This is where bind() becomes useful. Take a look at > >> > http://jsfiddle.net/5ncg3/1/ -- you'll see that in the console, when > we > >> > call > >> > "otherChecker.checkOtherOut", "this" now refers to our original > >> > ThisChecker > >> > instance. > >> > > >> > 3. The parentheses allow you to execute the function immediately, > >> > without > >> > bothering to assign it to a variable or make it a member of an object. > >> > Try > >> > executing > >> > (function() { console.log('a'); })(); > >> > in your console, and then try > >> > function() { console.log('b') }(); > >> > -- the second one will throw an error. There are loads of places that > >> > running a function as soon as it's declared is useful, as you've > noticed > >> > in > >> > the MooTools source! > >> > > >> > Hope this helped a bit! > >> > > >> > - Barry > >> > www.barryvan.com.au > >> > > >> > On 3/12/2010 8:10 PM, Maxim Lacrima wrote: > >> >> > >> >> Hi! > >> >> > >> >> I'm quite new to javascript and MooTools. > >> >> I have a class that looks like following: > >> >> > >> >> var FooComponent = new Class({ > >> >> elem: new Element('div'), > >> >> status: this.getCurrentStatus() > >> >> }); > >> >> > >> >> Two questions: > >> >> 1) When I access elem property as FooComponent.elem, will Element be > >> >> instantiated only once (when I access it for the first time) or is it > >> >> instantiated each time I access elem property? > >> >> 2) When I access status property, which object does 'this' belongs > to? > >> >> > >> >> And one more general javascript question. What does a function > wrapped > >> >> in parentheses mean in javascript? This can be often found in > MooTools > >> >> source code: > >> >> (function(){ > >> >> // function body > >> >> })(); > >> >> > >> >> Thanks in advance. > >> >> Sorry for my English. > >> >> > >> > > >> > > >> > -- > >> > Not sent from my Apple πPhone > >> > > >> > > >> > >> > >> > >> -- > >> with regards, > >> Maxim > > > > > > > > -- > with regards, > Maxim >
