JavaScript:箭头函数是一等函数吗?

标签 javascript

由于箭头函数没有 this,因此与常规函数相比,它们缺乏一个非常基本的能力:它们不能用作构造函数,因为它们没有上下文。

考虑到这一点,箭头函数是否被视为一等函数?

编辑:如果箭头函数被认为是一等函数,箭头函数的属性是什么让你仍然称它为一等?

最佳答案

Seeing as how arrow functions have no this, they lack a pretty fundamental ability compared to regular functions: They can't be used as constructors, because they have no context.

这只与函数的执行有关,与其类型无关。 this 值是执行的一个方面,而不是函数对象本身。

With this in mind, are arrow functions considered first class objects?

是的,它们和任何其他对象一样都是数据对象,唯一的区别在于它们包含可以调用的可执行代码。不同类型函数在调用方面的能力和限制与它们是否是一流对象没有任何关系。

const fn = foo => foo + 42
const fn2 = fn

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

console.log(fn(42))
console.log(fn2(42))

fn.foo = "bar"
console.log(fn2.foo) // "bar"


edit: If arrow functions are considered first class functions, what are the properties of arrow functions that allow you to still call it one?

与传统函数的属性相同。它们都是继承自 Function.prototype 的对象。它们的主要区别在于它们的调用,这同样不会改变它们是否是一流对象/函数的问题。它们可以语法上用于任何其他对象可以使用的地方。

关于JavaScript:箭头函数是一等函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46415079/

相关文章:

javascript - javascript对象的属性名设置保留字需要加引号吗?

javascript - 设置全局正文字体大小不起作用

javascript - 同时运行多个 Jssor Caption

javascript - 使用 jQuery 检索跨度中的数据,并使用 jQuery 插入该数据

javascript - 将json转为数组

javascript - PHP/JavaScript 电子邮件联系表

javascript - 使用 AngularJS 删除和更新 json 对象

javascript - JS : How to combine Object from two different arrays into one Array/Object

javascript - vue 上下文中的 getElementsByClassName

javascript - 在窗口对象上使用全局变量包装函数