This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Question about Javascript object members. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Question about Javascript object members." Watch "Question about Javascript object members." New topic
Author

Question about Javascript object members.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Hi,

I have below code. This is a kind of Function store that assigns each function an id and then store in the cache:



I thought add method should be able to access properties like nextId and cache which are member of same object. But I have to access it using object reference. Am I doing something wrong here or Is this the way it works?


Pawan Chopra
SCJP - DuMmIeS mInD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

The function context (what this "points" to), is completely dependent on how you call the function; not how you define the function.

So without seeing how you are calling the functions, we cannot comment on why this is not what you would expect.

This is outlined in depth in chapters 3 and 4 of my book: Secrets of the JavaScript Ninja. You should give it a read. Chapter 5 also discusses a way to use closures to force what the function context will be ahead of time.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Thanks Bear, I feel I am lucky to have you today . Actually I am reading your book and came across this example



So was just trying the example. Now I was expecting this to be store. Is it true?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

No, as I said; how the function is called determines what this turns out being; so you cannot assume that it will always be a reference to the object in which it was defined.

In this simple example, the store is used to make sure the functions can be found regardless of how the functions are called.

In the asserts, we call the functions as methods of store, so this will be store; in this case, at least.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Bear Bibeault wrote:No, as I said; how the function is called determines what this turns out being; so you cannot assume that it will always be a reference to the object in which it was defined.


I agree. But in this case where we have store.add(), this refers to store right? Or may be I should again read topic about Functional context in the book .
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

I got it! Sorry about that. And one more thing What a great book! Thanks for that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

In the asserts, we call the functions as methods of store, so this will be store; in those cases. But always assuming so would make this code fragile.

Be aware that in upcoming chapter 5, on closures, we'll talk about better ways to write similar code that won't depend upon hard-coding the name.

Kudos, by the way, for recognizing that hard-coding the name isn't the best way to go about this. I'm sure that you'll find chapter 5 interesting.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

pawan chopra wrote:And one more thing What a great book! Thanks for that.

You're welcome! I hope you get a lot out of it, and thanks for buying it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about Javascript object members.
 
Similar Threads
new to write code, pls help!
javascript call()
java script
getClass() on Object with null value
JavaScript functional programming