Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminology for different types of functions

There seem to be several different "groups" of function-like things in JavaScript. Here are my made-up names for them:

  • "Regular functions": These are callable with parens and with new. Most functions fall into this category.

  • "Constructor-only functions": These are callable with new only. For example, Image, DOMParser, etc.

  • "Non-constructor functions": These are callable with parens, but not with new. For example, Math.round, Function.prototype.

  • "Non-callable functions": These functions aren't callable at all. For example, window.constructor, Document, NodeList, etc.

What are the proper names for these different groups of functions?


I'm also trying to figure out if which "group" a function is in can be determined based on whether its [[Prototype]] (__proto__) property is set to Function.prototype.

  • Most "regular functions" will have Function.prototype, but it could be removed using non-standard __proto__.

  • Most "constructor-only functions" have Object.prototype. I can't find any cases where they have Function.prototype (so, no call or apply). Is this always the case / spec'd behavior?

  • "Non-constructor functions" seem to mostly have Function.prototype with the exception of Function.prototype itself. Are there other cases which have Object.prototype?

  • "Non-callable functions" seem to always have Object.prototype. Is that the case?


I'll respond to some answers here:

Regular function: function
Constructor: constructor
Non-constructor: method
Non-callable: interface

This isn't really what I'm looking for. "function" and "constructor" are of course correct in a general way, but I'm looking for something more specific ("non-constructor function", etc.).

"method" is no better than "function" in telling you it can't be called with new, and constructor doesn't get the point across that it can only be called with new.

In many cases, the non-callable functions are only accessible from the constructor property of a host object. I think most of these would more accurately be called "constructors" than "interfaces."


[Stuff about host objects vs native objects]

This is sort of on the right track. Taking this question (and the accepted answer, and its comments) into consideration, there seems to be some disagreement or confusion about whether user-defined functions are host or native objects.

To avoid that, let's just call user-defined functions UDFs, and not worry about whether they are host or native. So we have UDFs, host objects, and native objects.

  • "Regular functions"

    • Every UDF

    • Native objects like Object, String, etc.

    • Host objects like window.toString (tested in Chrome 18).

  • "Constructor-only functions"

    • No native objects?

    • Host objects like DOMParser, Image.

  • "Non-constructor functions"

    • Native objects like Math.round and Function.prototype.

    • Host objects like DOMParser#parseFromString.

  • "Non-callable functions"

    • No native objects?

    • Host objects like NodeList, DocumentFragment.

So it looks like there could be a connection between host objects and constructor-only / non-callable functions, but host vs native doesn't seem to apply to non-constructor and regular functions.

like image 336
Dagg Nabbit Avatar asked May 07 '12 06:05

Dagg Nabbit


1 Answers

I think what you are picking up on here is the nuances between native objects built in to EcmaScript and host objects, ones created and supplied by the developer.

The "Regular Functions" map to Host Objects, that is to say functions created using JS and "Constructor-only functions",Non-constructor functions" and "Non-callable functions" map to native objects built into the language at a lower level.

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

like image 59
marteljn Avatar answered Oct 03 '22 17:10

marteljn