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

52
Vistas
Why is this tiny library function returning undefined?

I want to build a tiny JavaScript library that can be used like this:

library('input').method()

(async () => {
  // const nlp = function (text = '', lexicon) {
  const library = function (text) {
    const options = {}

    options.addQuotes = function (text) {
      return `"${text}"`
    }

    return options
  }

  const result= library('test').addQuotes()
  console.log(result)
})()

library.addQuotes is suppose to surround text around double quotes.

So I thought result would be:

"test"

Instead I got:

"undefined" 

Why is this, and how to fix it?

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

0

By having an extra parameter you are creating an inner variable text (scoped inside the addQuotes function). You do not pass any value and JS has not problem with that, only it will be undefined. When JS looks for text variable, it finds this undefined value and returns that.

Remove that extra argument name and you are good.

(async () => {
  // const nlp = function (text = '', lexicon) {
  const library = function (text) {
    const options = {}

    options.addQuotes = function () {
      return `"${text}"`
    }

    return options
  }

  const result= library('test').addQuotes()
  console.log(result)
})()

about 4 years ago · Juan Pablo Isaza Denunciar

0

The text parameter in the addQuotes function is overshadowing the text variable in the outer scope.

Remove the parameter from the addQuotes function:

(async () => {
  // const nlp = function (text = '', lexicon) {
  const library = function (text) {
    const options = {}

    options.addQuotes = function () {
      return `"${text}"`
    }

    return options
  }

  const result= library('test').addQuotes()
  console.log(result)
})()

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