2

When setting a JavaScript variable's properties to a function is there anything special going on besides setting a variable as a pointer to a function's code?

Isn't this exactly what a C# delegate is?

I know in JavaScript you can directly assign a function to a variable like so:

var x = function() { return "Hello"; }

In C# it's a little different:

Func<string> x = () => { return "Hello"; };

However aren't these equivalent? Isn't directly assigning the function just sugar? Is JavaScript's concept of having "functions as objects" any different from languages such as C#?

Omar Elabd
  • 1,914
  • 1
  • 18
  • 23
  • 4
    both are object orientated lenguages, the difference is that c# is strongly typed and javascript isnt, they are equivalent as far as I know – bto.rdz Apr 03 '15 at 05:53
  • 1
    There are some subtle difference in anonymous functions between JS and C#, but they are very subtle and difficult to find. They are connected to how closures are handled... http://www.smartik.net/2013/07/Closures-CSharp-vs-JS-Same-But-Different.html But in the end unless you try to do strange things, the differences are more "how it is implemented" than "how it works when used" – xanatos Apr 03 '15 at 08:44
  • @bto.rdz I still think there's some debate regarding javascript being an object oriented language, but since that's irrelevant, i've modified the question. End of the day they are both function pointers correct? – Omar Elabd Apr 03 '15 at 17:41
  • What kind of differences are you asking about? They're two completely separate languages and paradigms, so its hard to see what basis you want them compared on. Also, regarding "isn't directly ... just sugar". Sugar for what? – Asad Saeeduddin Apr 03 '15 at 20:21
  • @Asad When i mentioned sugar I am referring to syntactic sugar. Please see http://en.wikipedia.org/wiki/Syntactic_sugar. – Omar Elabd Apr 03 '15 at 20:37
  • @OmarElabd I know what syntactic sugar is. The question remains, what are you claiming function assignment is syntax sugar for? Syntactic sugar refers to language features that do not add functionality, but allow something that can already be done with verbose syntax to be done more readably or concisely. So what does your example desugar to? – Asad Saeeduddin Apr 03 '15 at 20:44
  • @Asad Directly assigning a function directly to a variable through an equals operator is the syntactic sugar. – Omar Elabd Apr 03 '15 at 20:47
  • @OmarElabd For what? What is the alternative verbose syntax that this is a concise substitute for? – Asad Saeeduddin Apr 03 '15 at 20:48
  • @Asad In C# i can not write the following: Func x = public string ReturnMsg() { return "Hello" }. In that sense JavaScript provides an easier syntax for directly assigning a function to a variable. – Omar Elabd Apr 03 '15 at 20:51
  • @OmarElabd That is not what syntax sugar is. Please try reading the article you provided a link to. C# also provides the ability to assign a function to a local variable: `class Program { static void Test() { } static void Main() { Action test = Test; } }` works fine. It additionally provides the ability to create a anonymous functions using lambda syntax or the `delegate` keyword, which can similarly be assigned to local variables. – Asad Saeeduddin Apr 03 '15 at 20:58
  • `Func x = public string ReturnMsg() { return "Hello" }` is complete nonsense because the `public` access modifier means nothing in this context, and the return type can be inferred. – Asad Saeeduddin Apr 03 '15 at 20:58
  • @Asad I'm not going to argue the definition of syntactic sugar with you, if your having difficulty understanding, please read the wiki link i provided. My question is regarding differences in function pointers between JavaScript and C# not the definition of Syntatic Sugar. Please reserve your comments for something that adds value to the question. There are plenty of forums you can use to debate minutiae with others. Also the access modifier is a mistake on my part, the example is contrived anyway. But for your benefit Func x = string ReturnMsg() { return "Hello" } – Omar Elabd Apr 03 '15 at 21:03
  • @OmarElabd I did read the wiki link you provided. Here's what it says: "a construct in a language is called syntactic sugar if it can be removed from the language without any effect on what the language can do". This is not true of removing the assignment operator from either language. Other than that, there doesn't seem to be much to answer. What sort of differences are you looking for? There's the obvious "JS is not statically typed, C# is statically typed". Internally, methods are implemented differently. Both of these differences are probably not news to you. So what are you asking for? – Asad Saeeduddin Apr 03 '15 at 21:09
  • @OmarElabd As I said before `Func x = string ReturnMsg() { return "Hello" }` has redundant return type specification. The return type is specified on the LHS of the assignment. The fact that you can't add useless information to a statement doesn't mean C# doesn't support assigning delegates to local variables, as I showed in my snippet earlier. – Asad Saeeduddin Apr 03 '15 at 21:12
  • @Asad I am not referring to removing the assignment operator. In your example you assigned a function to a variable, but you first declared the function. The example in the question assigns the function directly to the variable. Specifically I was wondering if anyone had any insight into these two declaration and whether they were identical. At the end of the day are they both not function pointers? Is there something special that JavaScript does that C# doesn't? – Omar Elabd Apr 03 '15 at 21:13
  • @OmarElabd Both examples assign a function directly to a variable. The difference is how the functions are defined. In C# you can define a function the same way as Javascript, i.e. `function(p1, p2) { return "foo"; }` becomes `delegate(T1 p1, T2 p2) { return "foo"; }` or `(T1 p1, T2 p2) => "foo"`, whichever you prefer. You can also define functions as members of a class. You can then assign functions defined using any of these three approaches to a local variable, same as in JS. – Asad Saeeduddin Apr 03 '15 at 21:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74458/discussion-between-omar-elabd-and-asad). – Omar Elabd Apr 03 '15 at 21:18

1 Answers1

1

Javascript and C# can acheive a lot of the same tasks, they have similar concepts and some concepts which are very differnet, we can acheive a function as object in both of the languages, while its very common, popular and easy, while in c# its uncommon to put a delegate inside a variable as it does not fit well in the language's standarts and guidelines most times, C# offers more solutions for problems like that. as for you're question, javascript functions as objects is not something new, its the same as any languages which supports delegates, but specifically in javascript its a standard feature to use.

Ran Sasportas
  • 2,256
  • 1
  • 14
  • 21