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

166
Views
JavaScript: define a function inside file "foo" for use in imported file "bar"

I have a file (let's call it `foo`) that I import a module into using RequireJS (let's call it `bar`). Something like:
foo.js:

function doStuff() {
  require(['path/to/bar/'], function(bar) => { //code// })
}

However, bar calls a function (let's call it func) which is not defined in bar, only called in the code. How can I define func for use in bar? Something like:

bar.js

function doMoreStuff() {
  func()
}
foo.js

function doStuff() {
  require(['path/to/bar/'], function(bar) => { 
    bar.func = () => { console.log("hello world!") }
    bar.doMoreStuff()
  })
}

Note: I know bar.js isn't written as a module should be, pretend it is - everything works in my code but I get an error that the function isn't defined (because it isn't, hence the question on how to define it)

about 4 years ago · Juan Pablo Isaza
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!