0

Specifically, I'm interested in how to get the code below to work, logging as per the comments.

function someWeirdness(func) {
    var funcThis = undefined; // Instead of undefined, access to what,
                             // if anything, func is bound to
                             // (func.this doesn't work, but that would be
                             // it if it did).
    console.log(funcThis);
}
function greet(greeting) {
    console.log([greeting || "Hello", this.name].join(" "));
}
var obj = {
    name: "object"
};
greetObj = greet.bind(obj);
someWeirdness(greetObj); // logs obj

The application would be something more along the lines of:

function compositionalMagic(func, params) {
    params = Array.isArray(params) ?
                params : Array.prototype.slice.call(arguments, 1);
    // Additional processing and hooplah
    func.apply(func.this, params);
}
jameslafferty
  • 2,152
  • 2
  • 21
  • 25
  • 1
    Do you want to know what object a function is bound to, or do you want to log "obj"? because there is no relationship between the two. – Touffy Oct 20 '15 at 20:01
  • 1
    [You're using `bind` wrong](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind). – Joseph Oct 20 '15 at 20:01
  • Yeah... sorry guys... typo! (Or rather brain fart stringifying the param for bind). – jameslafferty Oct 20 '15 at 20:02
  • There's a few ways you could do this, but not as you have written here. Can you give context for practical application of what you're trying to accomplish? – spmurrayzzz Oct 20 '15 at 20:08
  • Ah, now I see what you're hoping for. sorry, it's not possible. The bound value of `this` is only available within the scope of each function. You'd have to expose it from within `greet`. – Touffy Oct 20 '15 at 20:10
  • 4
    Duplicate of http://stackoverflow.com/questions/14307264/what-object-javascript-function-is-bound-to-what-is-its-this – Will Oct 20 '15 at 20:11
  • maybe modify your greet to keep trace of bind – Anonymous0day Oct 20 '15 at 20:11
  • @Will is correct. My search-fu wasn't good enough to let me find it. – jameslafferty Oct 20 '15 at 20:20
  • `func.apply(func.this, params);` I don't understand why you'd need anything special there. If `this` is bound, then you can't change it anyway. Just do `func.apply(null, params);` –  Oct 20 '15 at 20:21
  • I'm actually just interested in being able to correctly (i.e., with the right ```this``` value) apply the function to an arbitrary list of parameters. – jameslafferty Oct 20 '15 at 20:23
  • @jameslafferty: But what is the right value? If you're saying that you want to leave a bound `this` as is, but otherwise set your own value, then you're good to go. Just do `func.apply(my_this, params)`, and `my_this` will be ignored if `this` is already bound. –  Oct 20 '15 at 20:25
  • @squint But... let's say I don't know what (or even whether) ```func``` already is bound to something. I'm looking to provide an API where I accept a function, bound or not, and a list or array of parameters, and then return a new function that invokes the original function with the expected context and parameters later. – jameslafferty Oct 20 '15 at 20:30
  • 1
    As I explained in my previous content, if the `this` of the function you receive is bound, and you use `apply()` to call that function, the `this` value you provide will be ignored and the bound one will be used. So if you want to honor the bound value, then you have no issue. ...[Here's a demo](http://jsfiddle.net/0rytr25q/1/) –  Oct 20 '15 at 20:36
  • ...though you're using terms like *"right"* and *"expected"* without defining them. I assume you mean you do want to honor the bound value instead of replacing it. –  Oct 20 '15 at 20:38

1 Answers1

0

this is only available inside of a given function scope — unless you intentionally leak the reference.

function foo( arg ) {
  arg.this = this;
}

var obj = { foo: 'bar' };
var obj2 = {};

foo.call( obj, obj2 );

console.log( obj2.this ); // logs { foo: 'bar' }

You could monkeypatch Function.prototype.bind to do something like the above every time you invoke bind, but probably not ideal.

spmurrayzzz
  • 136
  • 2