Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
"object is not extensible" while accessed via `import()` function

I'm importing my JavaScript "module" using the "dynamic import()" call:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"><title>Module</title><style>:root { background: #545454; }</style>
  <script>
    window.addEventListener('load', e => {
      import('./script.js').then(async module => {
        const fn = await module.myFunction();
        fn.whatever();
      });
    });
  </script>
</head>
<body></body></html>

and, this is the script.js file:

export async function myFunction() {
    this.whatever = function () {
        console.log('Ok');
    };
    return this;
}

By running it, Google Chrome complains that the "object is not extensible" while adding whatever:

script.js:2 Uncaught (in promise) TypeError: Cannot add property whatever, object is not extensible
    at Module.myFunction (script.js:2:19)
    at module.html:12:33

So, what's wrong and how to work around the issue?

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

module in the then callback on that import() is the module namespace object for the module. That's an object the JavaScript engine creates that has properties for the exports of the module. The MNO is not extensible; its purpose is to accurately reflect the exports of the module. If you could add properites to it (extend it), it wouldn't accurately reflect the module exports anymore.

When you call module.myFunction(), this during the call is set to module during the call — which is the non-extensible module namespace object. So the line this.whatever = ___ in myFunction fails, because that's trying to create a new property on a non-extensible object.


Just as an addition: It's not clear what you're trying to do with myFunction, but you could create a new object that uses the MNO as its prototype, and add a property to that.

export async function myFunction() {
    const obj = Object.create(this);
    obj.whatever = function () {
        console.log("Ok");
    };
    return obj;
}

I wouldn't recommend doing that without a good reason (and I'd remove the async unless the function uses await), but it would work.

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda