javascript - How to use await import() for an exported constructor function? - Stack Overflow

I have this as my function constructor:test.jsconst myConstructor = function(){this.hi = 'hi

I have this as my function constructor:

test.js

const myConstructor = function(){
  this.hi = 'hi'
  console.log(this.hi)
}
export default myConstructor

And I'm trying to await import() it in test2.js:

test2.js

const test = await import('./test.js')

new test()

This fails with TypeError: test is not a constructor.

However, if I import it normally it works fine:

test2.js

import test from './test.js'
//const test = await import('./test.js')

new test()

^^ prints hi to the console.

What is going on here? Why do the two approaches behave so differently for constructor functions? This doesn't appear to happen for normal functions.

I've had to work around this by calling the constructor function directly within the constructor function module, and this is not ideal.

I have this as my function constructor:

test.js

const myConstructor = function(){
  this.hi = 'hi'
  console.log(this.hi)
}
export default myConstructor

And I'm trying to await import() it in test2.js:

test2.js

const test = await import('./test.js')

new test()

This fails with TypeError: test is not a constructor.

However, if I import it normally it works fine:

test2.js

import test from './test.js'
//const test = await import('./test.js')

new test()

^^ prints hi to the console.

What is going on here? Why do the two approaches behave so differently for constructor functions? This doesn't appear to happen for normal functions.

I've had to work around this by calling the constructor function directly within the constructor function module, and this is not ideal.

Share Improve this question asked Aug 23, 2022 at 21:59 DshizDshiz 3,3514 gold badges35 silver badges63 bronze badges 2
  • console.log(test) and show us what you got – Konrad Commented Aug 23, 2022 at 22:01
  • 1 console.log(test) gives me this for await import(): [Module: null prototype] { default: [Function: myConstructor] } and this for normal import: [Function: myConstructor] – Dshiz Commented Aug 23, 2022 at 22:03
Add a ment  | 

2 Answers 2

Reset to default 6

Dynamic import returns:

It returns a promise which fulfills to an object containing all exports from moduleName, with the same shape as a namespace import (import * as name from moduleName): an object with null prototype, and the default export available as a key named default.

If its Promise only resolved to the default export, you wouldn't be able to use any named exports it may have. So, you need:

const testNamespace = await import('./test.js')

new testNamespace.default()

Async import returns module, so you need to use .default property to access default export of module.

const test = await import('./test.js').default

new test()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745050408a4608352.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信