FunctionConstructor在TypeScript中是什么?

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

What is FunctionConstructor in typeScript?

问题

I was going through the Typescript Ecmascript source code. I came across this:

interface FunctionConstructor {
/**
* Creates a new function.
* @param args A list of arguments the function accepts.
*/
new(...args: string[]): Function;
(...args: string[]): Function;
readonly prototype: Function;
}

declare var Function: FunctionConstructor;

I am assuming FunctionConstructor is the type of Function's constructor.

We declaring a Function variable which has FunctionConstructor interface. What are the first two parameters in the FunctionConstructor interface? And why would variable Function(a plain JavaScript object deriving from Object) have its type similar to its constructor?

Basically I am trying to understand what is happening behind the scenes. Any help is appreciated. Thanks!

英文:

I was going through the Typescript Ecmascript source code. I came across this:

interface FunctionConstructor {
    /**
     * Creates a new function.
     * @param args A list of arguments the function accepts.
     */
    new(...args: string[]): Function;
    (...args: string[]): Function;
    readonly prototype: Function;
}

declare var Function: FunctionConstructor;

I am assuming FunctionConstructor is the type of Function's constructor.

We declaring a Function variable which has FunctionConstructor interface. What are the first two parameters in the FunctionConstructor interface? And why would variable Function(a plain JavaScript object deriving from Object) have its type similar to its constructor?

Basically I am trying to understand what is happening behind the scenes. Any help is appreciated. Thanks!

答案1

得分: 1

首先看一下Function() 构造函数

Function() 构造函数创建一个新的 Function 对象。直接调用构造函数可以动态创建函数,但它与 eval() 一样存在安全性和性能问题(尽管后者的性能问题较少)。但与 eval 不同(它可以访问局部作用域),Function 构造函数创建的函数仅在全局作用域中执行。

const sum = new Function('a', 'b', 'return a + b');

console.log(sum(2, 6));
// 预期输出: 8

重新表述:动态创建函数是一个高级特性。如果可能的话,使用普通函数。

话虽如此:

请记住,变量和类型使用不同的命名空间。

此代码是合法的

interface Foo {
  id: number
}

var Foo = 1;

因此,var Function: FunctionConstructor 是一个对象,可用于生成 Function 对象。

接口 FunctionConstructor 模拟了如何使用 Function 对象:

  • 使用 new 创建函数
// 使用 new(...args: string[]): Function;
const sum = new Function('a', 'b', 'return a + b');
console.log(sum(2, 6));
  • 不使用 new 创建函数
// 使用 (...args: string[]): Function;
const sum = Function('a', 'b', 'return a + b');
console.log(sum(2, 6));

prototype: Function 是 JavaScript 中原型继承的标志。
通过 Function 对象创建的对象将其 __proto__ 设置为类型为 Function 的对象,因此可以使用该类型的方法(可以调用等)。

const sum = Function('a', 'b', 'return a + b');
console.log(sum(2, 6));

console.log(Function.prototype === sum.__proto__) // true
英文:

Firstly see Function() constructor

> The Function() constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval(). However, unlike eval (which may have access to the local scope), the Function constructor creates functions which execute in the global scope only.
> lang-js
> const sum = new Function('a', 'b', 'return a + b');
>
> console.log(sum(2, 6));
> // Expected output: 8
>

To rephrase: creating functions dynamically is an advanced feature. Use regular functions if possible.

Having said that:

remember that variables and types use different namespaces.

This code is legal

interface Foo {
  id: number
}

var Foo = 1;

Thus, var Function: FunctionConstructor is an object that can be used to produce Function objects.

Interface FunctionConstructor models how Function object can be used:

  • creating a function with new
// using new(...args: string[]): Function;
const sum = new Function('a', 'b', 'return a + b');
console.log(sum(2, 6));
  • creating a function without new
// using (...args: string[]): Function;
const sum = Function('a', 'b', 'return a + b');
console.log(sum(2, 6));

prototype: Function is a sign of prototypal inheritance in JS.
Object created via Function object will have their __proto__ set to object of type Function, and thus can use methods from this type (can be called etc).

const sum = Function('a', 'b', 'return a + b');
console.log(sum(2, 6));

console.log(Function.prototype === sum.__proto__) // true

huangapple
  • 本文由 发表于 2023年3月9日 20:10:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684430.html
匿名

发表评论

匿名网友

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

确定