- See more at: http://blogtimenow.com/blogging/automatically-redirect-blogger-blog-another-blog-website/#sthash.YsVbCxL0.dpuf Journey to Becoming a Web Developer: Anatomy of a JavaScript function

Monday, January 20, 2014

Anatomy of a JavaScript function

JavaScript functions are very different from Ruby methods. While reading Intro to Functions I became quite confused because I am so used to the way methods work in Ruby. I did understand some of it. Here is what I observed.

In JavaScript defining properties of pure functions is that they always return the same values when given the same arguments.
A function is built by first using the word function then the name we want to call the function followed by the names of the argument/s in parenthesis.  Last would be the body of the function. Ex:






Return is used to define the value that the function returns. If there is no expression after return the function will return undefined


In this example both the top level variable and the local variable are named variable.  The function printVariable is called inside of the function test. Even though printVariable was called inside of test the local variable is not available to the printVariable function.


Lexical Scoping is when a variable visibility is determined by the functions placement in the program text. Both local variables and top-level variables are visible. In the example because the child function was defined in the parent function the local variable from the parent function is the only variable visible to it.



No comments:

Post a Comment