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

234
Vistas
How to make a function in a function

I am somewhat new to Javascript, and I am trying to make a library for myself, so I dont have to code it in later. I have this code (below).

function lib() {
  let _this = this;
  this.addstring= (n, d) => {
    return n + d
  }
}
console.log(lib.addstring("foo", "bar"))

When the code above is ran, it tells me that lib.addstring is not a function. How would I be able to write this as a function?

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

0

You are using this incorrectly. You do not create lib instance, so you don't need this. If you wish to group everything on lib object, just wrap everything to the object:

const lib = {
  addstring(n, d) {
    return n + d
  }
}

console.log(lib.addstring("foo", "bar"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

A nice way to do this would be ES6 modules. With ES6 modules, you would create a new file that uses the export keyword for every function you want to use externally. Here's an example:

library.mjs

export function addstring(n, d) {
  return n + d;
}

code.js

import {addstring} from './library.mjs';

console.log(addstring("foo", "bar"))
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