ES6 导出默认,多个函数相互引用 [英] ES6 export default with multiple functions referring to each other

查看:50
本文介绍了ES6 导出默认,多个函数相互引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 es6 中你可以定义一个这样的函数模块

in es6 there you can define a module of functions like this

export default {
    foo() { console.log('foo') }, 
    bar() { console.log('bar') },
    baz() { foo(); bar() }
}

以上似乎是有效的代码,但是如果我调用 baz() 它会引发错误:

the above seems to be valid code, but if I call baz() it throws an error:

ReferenceError: foo 未定义

如何从另一个函数调用 foo?在这种情况下 baz

How do you call foo from another function? in this case baz

编辑

这是实际上不起作用的代码.我已经简化了代码,所以它只是需要的核心

Here's the code that actually doesn't work. I have simplified the code so it's only the core as needed

const tokenManager =  {
  revokeToken(headers) { 
    ... 
  },
  expireToken(headers) {
    ...
  },
  verifyToken(req, res, next) {
    jwt.verify(... => {
      if (err) {
        expireToken(req.headers)
      }
    })
  }
}

export default tokenManager 

错误是

expireToken(req.headers);
        ^
ReferenceError: expireToken is not defined

编辑 2

我只是尝试在 expireToken 之前添加 tokenManager 并且它终于起作用了

I just tried adding tokenManager before expireToken and it finally works

推荐答案

export default {...} 构造只是这样的快捷方式:

The export default {...} construction is just a shortcut for something like this:

const funcs = {
    foo() { console.log('foo') }, 
    bar() { console.log('bar') },
    baz() { foo(); bar() }
}

export default funcs

很明显,现在模块的作用域中没有 foobarbaz 函数.但是有一个名为 funcs 的对象(尽管实际上它没有名称)包含这些函数作为其属性,并将成为模块的默认导出.

It must become obvious now that there are no foo, bar or baz functions in the module's scope. But there is an object named funcs (though in reality it has no name) that contains these functions as its properties and which will become the module's default export.

因此,要修复您的代码,请在不使用快捷方式的情况下重新编写它,并将 foobar 作为 funcs 的属性引用:

So, to fix your code, re-write it without using the shortcut and refer to foo and bar as properties of funcs:

const funcs = {
    foo() { console.log('foo') }, 
    bar() { console.log('bar') },
    baz() { funcs.foo(); funcs.bar() } // here is the fix
}

export default funcs

另一种选择是使用 this 关键字来引用 funcs 对象,而无需显式声明它,正如@pawel 指出的.

Another option is to use this keyword to refer to funcs object without having to declare it explicitly, as @pawel has pointed out.

另一种选择(也是我通常更喜欢的选择)是在模块范围内声明这些函数.这允许直接引用它们:

Yet another option (and the one which I generally prefer) is to declare these functions in the module scope. This allows to refer to them directly:

function foo() { console.log('foo') }
function bar() { console.log('bar') }
function baz() { foo(); bar() }

export default {foo, bar, baz}

如果您想要方便的默认导出单独导入项目的能力,您还可以单独导出所有功能:

And if you want the convenience of default export and ability to import items individually, you can also export all functions individually:

// util.js

export function foo() { console.log('foo') }
export function bar() { console.log('bar') }
export function baz() { foo(); bar() }

export default {foo, bar, baz}

// a.js, using default export

import util from './util'
util.foo()

// b.js, using named exports

import {bar} from './util'
bar()

或者,正如@loganfsmyth 建议的那样,您可以不使用默认导出,只需使用 import * as util from './util' 将所有命名导出都包含在一个对象中.

Or, as @loganfsmyth suggested, you can do without default export and just use import * as util from './util' to get all named exports in one object.

这篇关于ES6 导出默认,多个函数相互引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆