使用 `Object.create` 创建函数

huangapple go评论55阅读模式
英文:

Creating functions using `Object.create`

问题

我正在学习JavaScript,我在理解函数的某个方面遇到了困难。例如,我知道我可以使用 Object.create 创建一个数组,就像这样:

const array = Object.create(Array.prototype);

由于在JavaScript中,函数也是对象,我想知道是否可以以类似的方式使用 Object.create 创建一个函数?

英文:

I am learning JavaScript and I am having trouble understanding a certain aspect of functions. For example, I know that I can create an array using Object.create like this:

const array = Object.create(Array.prototype);

Since functions are also objects in JavaScript, I'm wondering if it's possible to create a function using Object.create in a similar way?

答案1

得分: 4

Object.create 仅仅是一个方便的函数,用于配置原型链并定义自有属性。

函数的关键区别特征是存在非用户级的 [[Call]] 内部方法,可通过 identifier(<arguments>) 语法调用。我知道的唯一配置对象以具有这个内部方法的方式有:

  1. 使用通常的函数对象创建语法(例如 function() {}() => {}),
  2. 使用 Function 构造函数,或者
  3. 使用类并扩展 Function
const f = Object.create(Function.prototype)
console.log(typeof f) // 'object'(而不是 'function')
英文:

It is not possible.

Object.create is merely a convenience function to configure prototype chains and define own properties.

The key distinguishing characteristic of functions is the existence of the non-userland [[Call]] internal method, invokeable with the identifier(<arguments>) syntax. The only ways I know to configure an object to have this internal method are:

  1. to use the usual function-object creation syntaxes (eg. function() {}, () => {}), or
  2. Use the Function constructor, or
  3. Use classes and extend Function.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const f = Object.create(Function.prototype)
console.log(typeof f) // &#39;object&#39; (not &#39;function&#39;) 

<!-- end snippet -->

答案2

得分: 2

[Object.create](1) 不是用于直接创建函数的,而是用于创建具有特定原型的对象。

要在 JavaScript 中创建一个[函数](2),你可以使用函数声明或函数表达式语法。

在 JavaScript 中,函数被视为“第一类对象”,这意味着它们被视为对象并且具有属性和方法,就像任何其他对象一样。这种行为是由于 JavaScript 对高阶函数的支持以及它的函数式编程能力。

以下是函数在 JavaScript 中被视为对象的几个原因:

  1. 函数是 Function 构造函数的实例:在 JavaScript 中,函数是使用 Function 构造函数或函数声明/表达式创建的。Function 构造函数本身是一个对象,当你创建一个函数时,实际上是创建了该对象的一个实例。
  2. 函数可以有属性:与任何对象一样,函数可以有分配给它们的属性。你可以为函数添加自定义属性,访问或修改它们,甚至删除它们。
  3. 函数可以作为参数传递和从其他函数返回:在 JavaScript 中,函数可以作为参数传递给其他函数,并作为值从函数返回。这种行为是高阶函数的基本特性,与函数被视为对象的概念密切相关。
  4. 函数具有内置属性和方法:除了自定义属性,JavaScript 中的函数还具有内置属性和方法。例如,它们具有像 name 和 length 这样的属性,提供有关函数的信息,并且有像 call()、apply() 和 bind() 这样的方法,允许你控制函数的执行上下文。
英文:

Object.create is not intended for creating functions directly but is used for creating objects with specific prototypes.

To create a function in JavaScript, you can use the function declaration or function expression syntax.

In JavaScript, functions are considered "first-class objects," meaning they are treated as objects and have properties and methods like any other object. This behavior is due to JavaScript's support for higher-order functions and its functional programming capabilities.

Here are a few reasons why functions are considered objects in JavaScript:

  1. Functions are instances of the Function constructor: In JavaScript, functions are created using the Function constructor or function declarations/expressions. The Function constructor itself is an object, and when you create a function, you are essentially creating an instance of that object.
  2. Functions can have properties: Like any object, functions can have properties assigned to them. You can add custom properties to a function, access or modify them, and even delete them.
  3. Functions can be passed as arguments and returned from other functions: In JavaScript, functions can be passed as arguments to other functions and returned as values from functions. This behavior is a fundamental characteristic of higher-order functions and is closely tied to the concept of functions being objects.
  4. Functions have built-in properties and methods: In addition to custom properties, functions in JavaScript have built-in properties and methods. For example, they have properties like name and length, which provide information about the function, and methods like call(), apply(), and bind(), which allow you to control the function's execution context.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function myFunction() {
  // Function code here
}

myFunction.customProperty = &#39;Hello&#39;;

console.log(myFunction.customProperty);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 22:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定