nodein the shell and press the
Enterkey to start a REPL session).
x. Then the
typeofoperator conveys the current data type of the variable. The subsequent lines are all setting the variable to a different data type and then using typeof to display the change.
xvariable has its data type changed when a new type of data is assigned to it.
funcs.jsdeclare a function named
func1. Functions are similar to sub procedures in RPG. The fourth line is exporting
func1. To accomplish the same in RPG we place the
EXPORTkeyword on the P-spec of a sub procedure definition. Next create file
funcstest.jsso we can test our newly created module, as shown below.
funcstest.js, as shown below.
Ctrl+Shift+V) the above code into the REPL to see it run. After
func1is declared we see it being invoked and display the total variable via
console.log(...). Exit the Node.js REPL by typing
Ctrl+Shift+V) the below code into it. Notice the
func1()function is being invoked before it is declared.
http.createServer(...). This is an inline anonymous function, literally a chunk of code being passed as a parameter. The function could also have been separately declared and then reference the name on the call to
http.createServer(...), as shown below.
http.createServer(...)will be invoked, eventually. What do I mean by "eventually"? Well, first the code within
http.createServer(...)will run a bunch of other code, including parsing the query string or reading in HTML form variables, before it invokes the function passed as the second parameter. This is the "call back" concept, where a called function will "call back" into a function that was passed to it. Many times the callback will have parameters defined to receive input, in this case the
resvariables. RPG has a similar concept with procedure pointers.