| View previous topic :: View next topic |
| Author |
Message |
Poolkop
Joined: 01 Jun 2006 Posts: 123
|
Anyone familiar with Prototype.js?
I'm trying to unravel some of these secrets, mostly how to figure out what 'this' means in the different contexts...
What is a closure? it is a function that returns a function that encloses a reference to it's object? |
| |
|
|
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| It is a function with locally scoped variables that returns a function. When the function that gets returned is called it has access to those locally scoped variables - but nothing else does. |
| |
|
|
Poolkop
Joined: 01 Jun 2006 Posts: 123
|
| Thanks kadamat! |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
Poolkop, If you call the outside function again, the locally scoped variables are created anew, and the returned function has access to only those versions of those variables.
http://js.edea.se/events_and_closures.html might be helpful. |
| |
|
|
Poolkop
Joined: 01 Jun 2006 Posts: 123
|
| what's with the lack of the < symbol in the printVal() ? |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| No idea. Maybe an error? I wouldn't play with innerHTML myself |
| |
|
|
Poolkop
Joined: 01 Jun 2006 Posts: 123
|
Why would you write a function that takes 1 argument that is an object with the arguments attached to it?
Ie: function foo(args) { this.title = args.title; this.author = args.author; } ? |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
Because some arguments may be optional.
Or because you are using the object in other places |
| |
|
|
|
|