我知道如果我声称函数是数据类型,这对很多程序员来说都是一种亵渎。
但是如果你仔细想想,你不认为函数类似于 JS 中的数据类型。函数是 JS 中的一等公民。这不等同于理论上类似于数据类型的函数吗?
最佳答案
根据MDN , 函数是:
In JavaScript, functions are first-class objects, i.e. they are objects and can be manipulated and passed around just like any other object. Specifically, they are Function objects.
和
Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure—a set of statements that performs a task or calculates a value. To use a function, you must define it somewhere in the scope from which you wish to call it.
同时 data types是
Six data types that are primitives: Boolean Null Undefined Number String Symbol (new in ECMAScript 6) and Object
您还可以检查DevDocs查看更多关于函数以及它们如何成为对象的信息。并阅读此 Professional JS会让你的意见更清楚。 Javascript Garden作为 first-class
对象,可以更好地理解函数。
Object 是一种基本的 JavaScript 数据类型,Function Object 是它的类型之一。所以,答案是不,将 JS 函数称为数据类型是不对的。
关于javascript - 说JS函数可以是数据类型对不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32450939/