jcouyang / functional-javascript

DEPRECATED: moved to https://github.com/jcouyang/clojure-flavored-javascript
39 stars 7 forks source link

chapter 1.1 revise #11

Closed cameronbourke closed 8 years ago

cameronbourke commented 8 years ago

Really cool book! Just wanted to point out something I don't think is true. In chapter 1.1 you say

squareB directly defines a function using function statement that obviously has no return. While, squreA defines an anonymous function using function expression, and assigns the function returned from function expression to a variable named squareA

Both a named function and anonymous function have a return value. Consider the following:

console.log( function foo(x) { return x } );
console.log( (x) =>  x );

Both of these functions would return

the function
undefined

What are your thoughts?

jcouyang commented 8 years ago

Hi, while if you define a function using function statement, it return undefined

function a(){}

but when you put this code in a console.log as parameter, it become a function expression, not statement anymore.

anyfunction(function a(){})
//=> function a is expression
a= function b(){}
//=> function b is expression

but

function a(){}
// function a is statement
function(){}
// SyntaxError: function statement requires a name

because it's expect to be a statement, but statement must have a name

cameronbourke commented 8 years ago

Yeah you are right. I think I confused myself between a function statement and a named function expression.

let foo = function foo() {}

While the above is a named function, it is not a function statement. Is that correct?

jcouyang commented 8 years ago

yeah, that is correct. whenever a function is placed in a value's place, it's a expression, no matter it's named or anonymous function