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

251
Vistas
Init and use a js module in Laravel Blade

I am trying to do something simple in laravel. I have a simple js module but I am unable to init it with laravel (laravel-mix to compile). To test I want to do something as simple as:

<button onclick="console.log(Test.sum);">Print</button>
Uncaught TypeError: Cannot read properties of undefined (reading 'sum')

test.js

var Test = function() {
    var a = 0;
    var b = 1;

    var _sum = function() {
        return a + b;
    }

    return {
        sum: function() {
            _sum();
        }
    };
}();

app.js

const Test = require('./test');

My question, what is the propper way to init this module to use in my html web?

thanks a lot!

Solution:

test.js

var test = function() {
    var a = 0;
    var b = 1;

    var sum = function() {
        return a + b;
    }

    return {
        sum: function() {
            return sum();
        }
    };
}();

export { test }

app.js

import { test } from './test';
window.test = test ;
about 4 years ago · Juan Pablo Isaza
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