1

I have code that looks like this:

function doStuff(args) {
    // do stuff
    globalFunction(foo);
}

I want to refactor my code by passing the global function in as an argument, so that if the global function changes I can just change what is passed to my function:

function doStuff(args, fn) {
    // do stuff
    fn(foo);
}

But for some strange reason, the function now behaves differently, and I get an error somewhere in it. I even did this to double check that I'm passing it correctly:

function doStuff(args, fn) {
    // do stuff
    console.log(globalFunction === fn); // true
    fn(foo);
}

So I definitely know that it's the correct function. I'm really confused by this. Could someone give me an idea of what is going on?

EDIT: This just got weirder. I now have this:

function doStuff(args) {
    // do stuff
    globalFunction(foo); // works
    const fn = globalFunction;
    fn(foo); // doesn't work (???)
}

Maybe it's something specific to this function. Basically, the function is in a class, and it has references to this in its body. When I run the code normally it's fine, but when I run it from the assigned variable, I get an error where this is undefined. I would post the whole code but it's part of an external library which is huge, so I'm trying my best to summarise it.

EDIT 2:

I think I've isolated the issue. If I have code like

class foo {
    a() {
        console.log(this);
    }
}

Then const bar = new foo(); bar.a(); logs foo. However, if I do const fn = bar.a and then call bar.a(), I get undefined. Why does this happen?

Bluefire
  • 13,519
  • 24
  • 74
  • 118
  • Where is `foo` defined? Do you mean `fn(args)`? – guest271314 Nov 03 '17 at 03:58
  • what error are you getting? – ricky Nov 03 '17 at 03:58
  • show us the error that you getting. – Dean Nov 03 '17 at 03:59
  • please give in your real code as an example instead. Your example worked on my case – Prasanna Nov 03 '17 at 04:00
  • _"`// doesn't work (???)`"_ is not an adequate description of the issue. See https://stackoverflow.com/help/mcve – guest271314 Nov 03 '17 at 04:01
  • @everyone maybe it's something specific to this function. Basically, the function is in a class, and it has references to `this` in its body. When I run the code normally it's fine, but when I run it from the assigned variable, I get an error where `this` is undefined. – Bluefire Nov 03 '17 at 04:02
  • Then you need to set `this` for the function call – guest271314 Nov 03 '17 at 04:02
  • @guest271314 Why is it any different to calling it normally? – Bluefire Nov 03 '17 at 04:03
  • _"why?"_ Because that is the only viable description of the issue that you have provided thus far _"I get an error where `this` is undefined"_ – guest271314 Nov 03 '17 at 04:04
  • @guest271314 see my edit. – Bluefire Nov 03 '17 at 04:04
  • @guest271314 what do you mean "set `this` for the function call"? How is it possible that the function works normally but not when assigned to a variable? – Bluefire Nov 03 '17 at 04:05
  • The edited code does not provide further clarity. Again, you can set the context of a function call to an object, if that is the issue. What is `this` expected to be within the function body? – guest271314 Nov 03 '17 at 04:05
  • @guest271314 you mean by using `fn.apply`? Still, I don't see why assigning it changes the context? And I've explained above why I didn't provide the entire code. – Bluefire Nov 03 '17 at 04:06
  • Yes, you can use `.apply()` or `.call()` or other means to set `this` within the function. _"Still, I don't see why assigning it changes the context?"_ You have not stated what `this` is expected to be within the function call. Have you read the linked document at previous comment https://stackoverflow.com/questions/47088238/function-not-working-when-passed-as-an-argument#comment81126106_47088238? – guest271314 Nov 03 '17 at 04:06
  • @guest271314 see my edit. I've isolated the issue – Bluefire Nov 03 '17 at 04:10
  • You have created a new function reference – guest271314 Nov 03 '17 at 04:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158128/discussion-between-bluefire-and-guest271314). – Bluefire Nov 03 '17 at 04:13
  • Is `globalfunction` an accurate representation, or is it actually `someObject.someFunction`? – JLRishe Nov 03 '17 at 04:15
  • @JLRishe you are correct – Bluefire Nov 03 '17 at 04:39

1 Answers1

1
const fn = bar.a

creates a new function reference that is not the same as bar.a. You can use

fn.call(bar /* args */);

to set this to bar within the fn() call

class foo {
  a() {
    console.log(this, ...arguments);
  }
}

const bar = new foo();

const fn = bar.a;

fn.call(bar, 123);
guest271314
  • 1
  • 15
  • 104
  • 177