-2

im currently developing with requirejs and have a module wich returns two functions in an assoziative array, now I want to call the first function from the second one, I tried this.foo(), but it didn't work. Is there a way to call a sibling element in a assoziative array, or perhaps require the module in itself?

example:

define[],function(){
  return {
    foo: function() { echo("foo");},
    bar: function() { this.foo();}
  }
}
Marvin
  • 3
  • 1
  • 4
    Depends on how you're calling it exactly. Show that too. – deceze Sep 16 '15 at 15:32
  • 1
    The code in the question won't execute, due to syntax errors. To get good and relevant answers it helps to post code that is free syntax errors. – Louis Sep 16 '15 at 15:40

1 Answers1

1

Without seeing exactly how you're trying to call bar it's hard to know exactly what's going wrong. That said, this should work for you, assuming the problem is that this isn't referring to the right thing when calling bar():

define[], function(){
  var baz = {
    foo: function() { echo("foo");},
    bar: function() { baz.foo();}
  };

  return baz;
}

Since it basically "statically" does the same thing as Function.bind().

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • That worked, it seems, if I return the array anonymously like in my example `this` referes to Window, while defining it as a variable and calling the function on the Variable works. – Marvin Sep 17 '15 at 08:22
  • It's not about how you _return_ the object (it's an object, not an array) but how it's later used. – Matt Ball Sep 17 '15 at 14:12