简体   繁体   English

如何在 javascript 中将变量用作 function?

[英]How it is possible to use a variable as a function in javascript?

I'm trying to learn express.js and then i encountered these statements.我正在尝试学习 express.js,然后我遇到了这些陈述。

const express = require('express');
const app = express();

So now how is it possible to write this variable "express" as "express()".所以现在怎么可能把这个变量“express”写成“express()”。 Is it possible to do this with any type of variable?是否可以使用任何类型的变量来执行此操作?

Functions are first-class objects in JavaScript, they aren't special like they are in some other languages.函数是 JavaScript 中的一等对象,它们不像其他一些语言那样特殊。 They're just objects that are callable.它们只是可调用的对象。 Since they're objects, you can pass them around:由于它们是对象,因此您可以传递它们:

 function example() { console.log("Hi there"); } const e = example; e(); // "Hi there"

You can also put additional properties on them (something that Express also does).您还可以在它们上添加其他属性(Express 也可以这样做)。 Here's an example:这是一个例子:

 function example() { console.log("Hi there"); } example.prop = 42; const e = example; e(); // "Hi there" console.log(example.prop); // 42 console.log(e.prop); // 42 -- since `e` and `example` both refer to the same function

Is it possible to do this with any type of variable?是否可以使用任何类型的变量来执行此操作?

JavaScript variables don't have a type as they do in strongly-typed languages. JavaScript 变量没有强类型语言中的类型。 The value the variable holds has a type, but the variable doesn't;变量持有的值有类型,但变量没有; a variable can hold a number one moment, a string the next, a function after that...一个变量可以保持一个数字,接下来是一个字符串,然后是 function...

 function example() { console.log("Hi there"); } let e = example; e(); // "Hi there" e = 42; console.log(e); // 42 e = "Hi there"; console.log(e); // "Hi there"

If you want variables, parameters, and such to have types (so you can't put a string in a variable that's expected to contain a number), consider using TypeScript, Flow, or even just JSDoc combined with robust IDE support.如果您希望变量、参数等具有类型(因此您不能将字符串放入预期包含数字的变量中),请考虑使用 TypeScript、Flow,甚至只是结合强大的 IDE 支持的 JSDoc。 These layer a type system on top of JavaScript (to varying degrees).这些在 JavaScript 之上(在不同程度上)将类型系统分层。

Getting back to your question: You can do it with any variable , but not with any type of value.回到您的问题:您可以使用任何变量来执行此操作,但不能使用任何类型的值。 You can't call a number:您不能拨打号码:

 let e = 42; e(); // TypeError: e is not a function

You can only call functions or other host-provided callable objects¹ (which don't absolutely have to be true functions, but it's uncommon now for hosts to provide callable objects that aren't actual functions, it was problematic).您只能调用函数或其他主机提供的可调用对象¹(它们不一定是真正的函数,但现在主机提供不是实际函数的可调用对象并不常见,这是有问题的)。


¹ Specifically, in spec terms, any object that supports the [[Call]] internal operation . ¹ 具体而言,在规范方面,任何支持[[Call]] 内部操作的 object 。

It is not possible to do this with any variable but there is something called arrow functions for example不可能对任何变量执行此操作,但例如有一种称为箭头函数的东西

const test = () => {} and you can call this function by saying const test = () => {}你可以这样称呼这个 function

test()

within () it takes input and within {} you specify what it does or returns在 () 内它接受输入,在 {} 内你指定它做什么或返回什么

require fetches whatever you specified so i'm guessing it is equal to a function要求获取您指定的任何内容,所以我猜它等于 function

To do this with any variable it must be an arrow function.要对任何变量执行此操作,它必须是箭头 function。 Hope I helped you!希望我对你有所帮助!

Definitely yes, you are talking about JavaScript inline functions , check this out !绝对是的,你说的是 JavaScript内联函数看看这个

For example, you can do:例如,您可以这样做:

function example() {
  return 1;
}

const execute = example();

console.log(execute) // returns 1

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

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