O'Reilly Forums: Chapter 5 Javascript - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 5 Javascript Having trouble understanding the javascript in this chapter

#1 User is offline   cxgslegend 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-June 11

Posted 09 June 2012 - 11:22 PM

First off let me say that I really love the way the authors of this book explained javascript because I tried teaching myself about a year ago by trying to read "Head First Javascript" (Arghh, I hate that book so much) and just couldn't do it I just keept getting more and more confused so I had to buy and read "A Beginners's Guide Javascript by John Pollock"(this book was actually ok) after reading that I was able to go back and drag myself through the "Head First Javascript" book, however I never really felt like I understood Javascript and tried my best to avoid it. When I started reading "Head First HTML5" I wasn't expecting to see a lot of Javascript, but im glad they put it in, because they cleared up some of the things I was confused about. like what made the objects that I made different than the one's Javascript has built in. And I finally understand what happens when you pass in a object into a parameter, I always thought to myself what is the function actually seeing is it seeing like just a long string with all the objects properties or something, but they cleared it up, also I never understood what the ".this" statement really did but now I do. I swear I learned more javascript in the first four chapters of this book than both of the other books I read combined. However I am getting a little off topic (I just wanted to say that I love everything I have read in this book so far) I do have some questions.

1. What is a API? is it like an constructor function or is it just an object or something else?

2. Also in the getCurrentPosition(displayLocation, displayError) event I am having trouble understanding whats happening. How are you able to call the function displayLocation from within the parameters of an method? and when you call it how does it know to pass the object through the position part of displayLocation(position) when you didn't pass any parameters when you called it. For instance you didn't say getCurrentPosition(displayLocation(passobject), displayError) and yet it somehow still passed the object through the position variable of the displayLocation function.

I know I have been long winded, i'm done, promise.
0

#2 User is offline   Beth Robson 

  • Veteran Member
  • PipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Employee
  • Posts: 376
  • Joined: 17-September 08
  • Gender:Female
  • Location:Seattle, WA
  • Interests:I am interested in computer science, the Web, social media and the internet, Mac development, and adult learning.

Posted 10 June 2012 - 04:35 PM

Thanks for your comments, and great questions!!

1. An API is an Application Programming Interface, and it just means that if you write some software that can be used by another piece of software, the API is what specifies how to do it.

When you use the built-in JavaScript functions, you're using an API, because you are using functions running in the browser (a program) that are designed to be used by other programs (your JavaScript program). We often call collections of functions that work together to achieve some purpose--like Geolocation--an API, as in "The Geolocation API". And all that means is there is some documentation somewhere (in this case, the HTML5 spec) that tells you *how* to use those Geolocation functions.

Does that make sense?

2. When you write getCurrentPosition(displayLocation, displayError) , you are not calling the displayLocation and displayError functions, you are passing them as values! I know it seems odd to pass a function as a value, but that is actually one of the powerful things about JavaScript (not all programming languages allow you to do this!). Think of the parameters displayLocation and displayError as names for values, just like any other parameter that expects a value (whether thats a number, a string, an object, or a function!). Because getCurrentPosition() is a function in JavaScript, rather than one you write yourself, you don't see getCurrentPosition() actually calling those functions. If you could see inside getCurrentPosition(), you'd see it call one of the functions, say displayLocation() and of course it would be passing in your position (as an object).

You can write your own functions that take other functions as arguments. For example:

function f(g) {
return g(1);
}

function add1(x) {
return x + 1;
}

f(add1);

You'll get 2. Kind of mind-bendy, right? So what's happening here is that you are creating two functions: add1 takes a number, x, and adds 1 to it and returns it. Function f takes a function, which you give the name g. f calls g, and passes a number, 1, into it.

So, when you pass the function value add1 to f, f uses the parameter name g as the name for the function *value* (remember, JavaScript is pass by value!!) and calls g, with the result that you get the number 2.

I hope that makes sense and helps clear things up a bit. The function/value/passing a function to another function is a very complex topic that deserve a more in-depth treatment for sure, and we just didn't have room in HF HTML5 Programming to delve into it.

Elisabeth
Elisabeth Robson
Co-founder, WickedlySmart.com
Author: Head First JavaScript Programming, Head First HTML5 Programming, Head First HTML and CSS, Head First Design Patterns
0

#3 User is offline   cxgslegend 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-June 11

Posted 10 June 2012 - 09:55 PM

View PostBeth Robson, on 10 June 2012 - 04:35 PM, said:

Thanks for your comments, and great questions!!

1. An API is an Application Programming Interface, and it just means that if you write some software that can be used by another piece of software, the API is what specifies how to do it.

When you use the built-in JavaScript functions, you're using an API, because you are using functions running in the browser (a program) that are designed to be used by other programs (your JavaScript program). We often call collections of functions that work together to achieve some purpose--like Geolocation--an API, as in "The Geolocation API". And all that means is there is some documentation somewhere (in this case, the HTML5 spec) that tells you *how* to use those Geolocation functions.

Does that make sense?

2. When you write getCurrentPosition(displayLocation, displayError) , you are not calling the displayLocation and displayError functions, you are passing them as values! I know it seems odd to pass a function as a value, but that is actually one of the powerful things about JavaScript (not all programming languages allow you to do this!). Think of the parameters displayLocation and displayError as names for values, just like any other parameter that expects a value (whether thats a number, a string, an object, or a function!). Because getCurrentPosition() is a function in JavaScript, rather than one you write yourself, you don't see getCurrentPosition() actually calling those functions. If you could see inside getCurrentPosition(), you'd see it call one of the functions, say displayLocation() and of course it would be passing in your position (as an object).

You can write your own functions that take other functions as arguments. For example:

function f(g) {
return g(1);
}

function add1(x) {
return x + 1;
}

f(add1);

You'll get 2. Kind of mind-bendy, right? So what's happening here is that you are creating two functions: add1 takes a number, x, and adds 1 to it and returns it. Function f takes a function, which you give the name g. f calls g, and passes a number, 1, into it.

So, when you pass the function value add1 to f, f uses the parameter name g as the name for the function *value* (remember, JavaScript is pass by value!!) and calls g, with the result that you get the number 2.

I hope that makes sense and helps clear things up a bit. The function/value/passing a function to another function is a very complex topic that deserve a more in-depth treatment for sure, and we just didn't have room in HF HTML5 Programming to delve into it.

Elisabeth


Hey Elisabeth, thank you so much. It all make so much since now!

Maybe one day you should write a Javascript book. I would buy it in a heartbeat. For some reason every book I have ever read on Javascript, is written in a way like they almost expect that you are a professional Java developer who just needs a crash course on Javascript.

Anyways thanks, your books are awesome! :)

This post has been edited by cxgslegend: 10 June 2012 - 09:55 PM

0

#4 User is offline   Beth Robson 

  • Veteran Member
  • PipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Employee
  • Posts: 376
  • Joined: 17-September 08
  • Gender:Female
  • Location:Seattle, WA
  • Interests:I am interested in computer science, the Web, social media and the internet, Mac development, and adult learning.

Posted 12 June 2012 - 04:33 PM

Glad that helped. Would you mind if I posted your questions on wickedlysmart.com as "questions from a reader"? They are great questions.

Thanks

Elisabeth
Elisabeth Robson
Co-founder, WickedlySmart.com
Author: Head First JavaScript Programming, Head First HTML5 Programming, Head First HTML and CSS, Head First Design Patterns
0

#5 User is offline   cxgslegend 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-June 11

Posted 12 June 2012 - 05:28 PM

Absolutely. Feel free to post it.

Thanks :)
0

#6 User is offline   Dream Of Sleeping 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 04-June 12

Posted 20 June 2012 - 10:44 PM

View Postcxgslegend, on 10 June 2012 - 09:55 PM, said:

Hey Elisabeth, thank you so much. It all make so much since now!

Maybe one day you should write a Javascript book. I would buy it in a heartbeat. For some reason every book I have ever read on Javascript, is written in a way like they almost expect that you are a professional Java developer who just needs a crash course on Javascript.

Anyways thanks, your books are awesome! :)


I have to say, that as someone who has quite a bit of experience in Java, that I agree with you here totally. Every tutorial on Javascript or just about any programming language I have read really just expects you to already have some programming experience. Either that or the authors of those tutorials have forgotten how confusing it is when starting.

When I first started learning Java (not javascript) that was the hardest part for me, just the very beginning. I thought I just wasn't bright enough because I couldn't follow Head First java. Now I have more experience learning new programming languages is a breeze most of the time. So stick with it.

I do think the Head First series, when releasing a book on a particular programming language, should do what the Deitel books do - have one book for programmers and one teaching you how to program that assumes you know nothing. I think that would make the job much easer both on the author and the peole learning from it.

One thing I've noticed that the more complex the topic the better the Head First series is explaining it. I found polymorphism a breeze in the Java book, but boolean variables (like this book) only got a very quick (blink and you miss it) explanation in a side note.

I can appreciate what an incredibly tough job it is to write these kinds of books, so I don't mean any disrespect. Teaching programming is so hard because you need to know so much at the same time before any of it makes sense. But I do think the Head First series needs to commit to who it's audience is for each book.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users