File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Javascript object members.

 
pawan chopra
Ranch Hand
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pawan chopra
Ranch Hand
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it! Sorry about that. And one more thing What a great book! Thanks for that.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic