Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!