简体   繁体   English

不同类型功能的术语

[英]Terminology for different types of functions

There seem to be several different "groups" of function-like things in JavaScript. 在JavaScript中似乎有几个不同的“组”类似函数的东西。 Here are my made-up names for them: 以下是我的名字:

  • "Regular functions": These are callable with parens and with new . “常规函数”:这些函数可以使用parens和new来调用。 Most functions fall into this category. 大多数功能都属于这一类。

  • "Constructor-only functions": These are callable with new only. “仅构造函数”:这些函数只能使用new来调用。 For example, Image , DOMParser , etc. 例如, ImageDOMParser等。

  • "Non-constructor functions": These are callable with parens, but not with new . “非构造函数”:这些函数可以用parens调用,但不能用new调用。 For example, Math.round , Function.prototype . 例如, Math.roundFunction.prototype

  • "Non-callable functions": These functions aren't callable at all. “不可调用函数”:这些函数根本不可调用。 For example, window.constructor , Document , NodeList , etc. 例如, window.constructorDocumentNodeList等。

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 . 我还试图弄清楚函数所在的“组”是否可以根据其[[Prototype]]__proto__ )属性是否设置为Function.prototype

  • Most "regular functions" will have Function.prototype , but it could be removed using non-standard __proto__ . 大多数“常规函数”都有Function.prototype ,但可以使用非标准__proto__删除它。

  • Most "constructor-only functions" have Object.prototype . 大多数“仅构造函数的函数”都有Object.prototype I can't find any cases where they have Function.prototype (so, no call or apply ). 我找不到任何具有Function.prototype (因此,没有callapply )。 Is this always the case / spec'd behavior? 这总是情况/规格行为吗?

  • "Non-constructor functions" seem to mostly have Function.prototype with the exception of Function.prototype itself. “非构造函数”似乎主要具有Function.prototype ,但Function.prototype本身除外。 Are there other cases which have Object.prototype ? 还有其他具有Object.prototype吗?

  • "Non-callable functions" seem to always have Object.prototype . “不可调用的函数”似乎总是具有Object.prototype Is that the case? 是这样的吗?


I'll respond to some answers here: 我会在这里回答一些答案:

Regular function: function 常规功能: function
Constructor: constructor 构造函数: constructor
Non-constructor: method 非构造函数: method
Non-callable: interface 不可调用: 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 . “办法”并不比“功能”在告诉你它不能被称为更好的new ,和constructor不抓住了关键,它只能被称为new

In many cases, the non-callable functions are only accessible from the constructor property of a host object. 在许多情况下,只能从宿主对象的constructor属性访问不可调用的函数。 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. 为了避免这种情况,我们只需调用用户定义的函数UDF,而不用担心它们是主机还是本机。 So we have UDFs, host objects, and native objects. 所以我们有UDF,宿主对象和本机对象。

  • "Regular functions" “常规功能”

    • Every UDF 每个UDF

    • Native objects like Object , String , etc. 本地对象,如ObjectString等。

    • Host objects like window.toString (tested in Chrome 18). 主机对象如window.toString (在Chrome 18中测试)。

  • "Constructor-only functions" “仅构造函数”

    • No native objects? 没有原生物体?

    • Host objects like DOMParser , Image . 主机对象,如DOMParserImage

  • "Non-constructor functions" “非构造函数”

    • Native objects like Math.round and Function.prototype . Math.roundFunction.prototype这样的本机对象。

    • Host objects like DOMParser#parseFromString . 主机对象如DOMParser#parseFromString

  • "Non-callable functions" “不可调用的函数”

    • No native objects? 没有原生物体?

    • Host objects like NodeList , DocumentFragment . 主机对象如NodeListDocumentFragment

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. 因此看起来主机对象和仅构造函数/不可调用函数之间可能存在连接,但主机与本机似乎不适用于非构造函数和常规函数。

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. 我认为你在这里所看到的是EcmaScript内置的本机对象与主机对象之间的细微差别,这些对象是开发人员创建和提供的对象。

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. “常规函数”映射到主机对象,也就是说使用JS和“仅构造函数”创建的函数,非构造函数“和”不可调用函数“映射到较低级别的语言中内置的本机对象。

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

I don't think there's an official naming for what you're asking, but usually this is how people call all those functions in any programming language: 我认为没有正式命名你所要求的,但通常这是人们用任何编程语言调用所有这些函数的方式:

  • Regular function: function 常规功能: function
  • Constructor: constructor 构造函数: constructor
  • Non-constructor: method 非构造函数: method
  • Non-callable: interface (in the case of JS, this refers to native objects at a lower level) 不可调用: interface (在JS的情况下,这指的是较低级别的本机对象)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM