ES6导出默认,多个功能引用彼此 [英] ES6 export default with multiple functions referring to each other
问题描述
export default {
foo(){ console.log('foo')},
bar(){console.log('bar')},
baz(){foo(); bar()}
}
以上似乎是有效的代码,但如果我打电话 baz()
它会引发错误:
ReferenceError:foo未定义
如何从另一个函数调用 foo
?在这种情况下 baz
修改
这是实际上不起作用的代码。我已经简化了代码,所以它只是需要的核心
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未定义
编辑2
我刚刚尝试在 expireToken
tokenManager >最后工作
export default {...}
构造只是一个这样的快捷方式:
const funcs = {
foo(){console.log('foo')},
bar(){console.log('bar')},
baz(){foo(); bar()}
}
导出默认功能
它必须变得明显,现在没有 foo
, bar
或 baz
函数在模块的范围内。但是有一个名为 funcs
(虽然实际上没有名称)的对象包含这些函数作为其属性,并将成为模块的默认导出。
因此,要修复代码,请不要使用快捷方式重新编写代码,并参考 foo
和 bar
作为 funcs
的属性:
const funcs = {
foo(){console.log('foo')},
bar(){console.log('bar')},
baz(){funcs.foo(); funcs.bar()} //这里是修复
}
导出默认funcs
另一个选择是使用这个
关键字来引用 funcs
对象而不必声明它明确地说, as @pawel已经指出。
另一个选项(和我一般喜欢的)是在模块范围中声明这些函数。这可以直接引用它们:
function foo(){console.log('foo ')}
function bar(){console.log('bar')}
函数baz(){foo(); bar()}
export default {foo,bar,baz}
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() }
}
the above seems to be valid code, but if I call baz()
it throws an error:
ReferenceError: foo is not defined
How do you call foo
from another function? in this case baz
Edit
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
and the error is
expireToken(req.headers);
^
ReferenceError: expireToken is not defined
Edit 2
I just tried adding tokenManager
before expireToken
and it finally works
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
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.
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
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 }
这篇关于ES6导出默认,多个功能引用彼此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!