wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes problem understanding this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem understanding this" Watch "problem understanding this" New topic
Author

problem understanding this

simon tiberius
Greenhorn

Joined: Oct 30, 2012
Posts: 29


what do "this" in ??1 and ??2 refer to? the function.prototype? the anonymous function within which "this" is declared? or any object calling the function? or what? thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

This is an example from chapter 5 on closures from Secrets of the JavaScript Ninja. It's sort of difficult for anyone to understand out of context. It shows how to use closures to use currying in JavaScript.

Because partial is a method placed on the Function prototype, it will be invoked on a function and hence, the function context (the first this) of the method will be that function. We store that in the fn variable for later reference.

The second this is within the anonymous function that we return as the result, and hence it refers to whatever function context is assigned to that function when it is called. We don't know what it is and we don't care. (Remember, the function context is determined by how the function is called, not by how it is declared.) We just make it the function context of the curried function by passing it as the first parameter to apply().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
simon tiberius
Greenhorn

Joined: Oct 30, 2012
Posts: 29
so, both this refer to different entity? could you please give me an sample code that makes use of this partial function? perhaps I can understand more which "this" refers to what. Thanks a lot.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

simon tiberius wrote:so, both this refer to different entity?

Yes. Notice that they are within different functions. Each function invocation has it's own function context (this). And the function context (this) is not shared as part of a closure.

Could you please give me an sample code that makes use of this partial function? perhaps I can understand more which "this" refers to what.

Since you posted the code, I assume that you have bought a copy of the MEAP book. Please read that section thoroughly.
simon tiberius
Greenhorn

Joined: Oct 30, 2012
Posts: 29


this is the code from the book. I wanted another example since I'm still not very familiar with timer function in javascript and the book used setTimeout. So, what do the first and second this refer to in this code?
my guess is ??1 refers to setTimeout, and I really have no idea what ??2 refers to.
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem understanding this