I am learning Javascript and I have read functions are first class objects and most authors mention that functions can return functions (which are objects) and pass them as parameters to other functions.
I am sure there is a lot more to it so what are the differences between functions in C# and functions in javascript?
In C# am I right to say functions are not objects (don’t have methods, properties etc) even though with closures (with lambda expressions and delegates) they seem to behave like function objects as in javascript?
I feel that with lambda expressions in C# the distinction becomes a bit blurry for someone just coming to the language.
The one biggest difference I can think of is that in C# functions, variables are lexically scoped, whereas Javascript, variables are lexically scoped except for
this, which is dynamically scoped.For example, in Javascript, you can say something like
Try writing this in C#.
Or actually, don’t bother — it will not make any sense.
Why? Because
thisdoesn’t refer to anything here, lexically! Its meaning changes dynamically, unlike in C#, where its meaning simply depends on the enclosing class, not who the caller actually is.