Signature

Topics

1
Signature
2
Side Effects
3
Determinism
4
Scope
Continue Later

A signature (type, interface) of a function is an abstract concept in JavaScript. People stumble upon "function type" definition because they apply typeof to any function and get "function" in return. That's a problem of granularity, partially stemming from a dynamic type system. To overcome this, we can upgrade ourselves to TypeScript or just teach ourself to see more than the compiler.

A function signature is defined as a combination of input types, their number, and the result type. You should be very attentive to function signatures because they establish a contract between two types of code: the library code and the client code:

// Library
function add(x, y) {
  return x + y
}

// Client #1 -- may be another file
add(1, 2)

// Client #2 -- may be another project
add(3, 4)

The term "library" does not mean you should publish this code as a real library. But it determines a possibility. The term "client" has little to do with "client-server" interaction. It's just words having their own (sometimes interleaving) meanings. There's always a context to consider. So try to understand and remember those terms, as you'll see them pretty often.

Function signature is a contract between a "library" function and a "client" place where it's called. A set of such contracts is called API. API usually belongs to a whole library, so sometimes we can say about async and sync APIs of the same library. For now, you can imagine a library as just a separate file (module) or a separate place in a file.

Topics

1
Signature
2
Side Effects
3
Determinism
4
Scope
Continue Later