Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
How to import prototype getter from custom library using Object.defineProperty()

I want to create a reusable library for my projects. For this exercise, I've got one main library, and then an extension library for "identity" related functionality.

The idea is that I can include the identity module if / when needed, and extend the MainLibrary with a getter to access anything related to identity. Here's my code:

// Main library - index.js (Package name: @mainlib)
export class MainLibrary
{
   // ....
}

// Identity library - index.js (Package name: @mainlib/identity)
import { MainLibrary } from '@mainlib';

export function useIdentity()
{
  Object.defineProperty(MainLibrary.prototype, "identity", {
    get: function()
    {
       // Returns an identity object
       return {
          isAuth: function()
          {
             return false;
          }
       }
    },
    enumerable: true
 });
}

// App
import { MainLibrary } from '@mainlib';
import { useIdentity } from '@mainlib/identity';

useIdentity();

const myLib = new MainLibrary();
myLib.identity.isAuth(); // Throws error, identity is undefined

If I move the Object.defineProperty code into my App, it works just fine. How can I define this in the extension library and apply it to the MainLibrary class prototype, upon calling useIdentity() in my App?

Oh, I'm building with ViteJS, if that's relevant.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your MainLibrary object in your app is not the MainLibrary object you import in your extension, because that imports a local version of that by itself.

To solve the issue, instead make useIdentity accept an argument (effectively turning it into what is called a mixin):

// Identity library - index.js (Package name: @mainlib/identity)
export function useIdentity(context)
{
  Object.defineProperty(context.prototype, "identity", {
    get: function()
    {
       // Returns an identity object
       return {
          isAuth: function()
          {
             return false;
          }
       }
    },
    enumerable: true
 });
}

and then use it in your app:

// App
import { MainLibrary } from '@mainlib';
import { useIdentity } from '@mainlib/identity';

useIdentity(MainLibrary);

const myLib = new MainLibrary();
myLib.identity.isAuth(); // false
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda