Skip to main content
7 of 17
Changed samples to use static methods instead of instance methods; clarified question
Zev Spitz
  • 693
  • 1
  • 5
  • 19

Differences between "function as first-class citizen" mechanisms

C# / VB.NET have the concept of a delegate which enables first-class functions:

Action action = Console.WriteLine;

This is not the method itself, but rather an object pointing to the method. Thus, there can be multiple objects that point to the same method + target:

Action action2 = Console.WriteLine;
Object.ReferenceEquals(action, action2); //False

(although comparing the two objects with == will return True.)

Javascript does not require delegates, because a function is itself an object which can be passed around. Therefore any two references to the function will refer to the same object:

var fn = console.log;
var fn2 = console.log;
console.log(fn === fn2); //true

What different capabilities arise from these two mechanisms -- "function as object" and "pointer-to-function as object"?

Zev Spitz
  • 693
  • 1
  • 5
  • 19