1

I know that you can bind the this value of a function using

function.bind(new_this_value, arguments)

But is there a way to access the bound value? ie something like this:

console.log(my_function.boundValue)

In other words, suppose a module provides the following function:

function getACoolFunction () {
  var someFarAwayFunction = function() {console.log(this.name)}
  var bound_this_value = {name: "bound this value"}
  someFarAwayFunction.bind(bound_this_value)
  return someFarAwayFunction;
}

and I have this in my code:

import {getACoolFunction} from coolModule

var coolFunction = getACoolFunction();
// coolFunction.bound_value

How do I get the bound value of coolFunction from my code without changing the module?

CamJohnson26
  • 1,119
  • 1
  • 15
  • 40

1 Answers1

1

Nothing prevents you from doing this:

let my_function = function.bind(new_this_value, arguments);
my_function.boundValue = new_this_value;

As from the standard, a bound function is:

an exotic object that wraps another function object.

It has internal properties containing original function, provided this and arguments.
As they are explicitly mentioned as internal, I would say that they are not directly exposed and accessible.


Probably this Q/A contains details that can help you to work around this limitation.

Community
  • 1
  • 1
skypjack
  • 49,335
  • 19
  • 95
  • 187
  • Sure but I'm wondering if there's a way to get new_this_value without defining your own property, particularly if you're debugging objects you didn't write for example – CamJohnson26 Feb 13 '17 at 21:31