I'm going to assume method
is actually fn
, and that functionB
is a function assigned to an object property, and that the various syntax errors aren't present in your actual code:
var obj = {
name: "foo",
functionB: function() {
console.log(this.name); // <== Using `this` to refer to `obj`
}
};
function functionA(fn) {
return function functionC(params) {
var customparams = [params, {something: "else"}];
return fn.apply(null, customparams);
};
}
var load = functionA(obj.functionB);
load("a", "b"); // Fails because `this` in the call to `functionB` isn't `obj`
If so, you can fix it in a couple of wasy:
1) By using Function#bind
whne passing functionB
into functionA
:
var load = functionA(obj.functionB.bind(obj));
load("a", "b"); // Works
var obj = {
name: "foo",
functionB: function() {
snippet.log(this.name); // <== Using `this` to refer to `obj`
}
};
function functionA(fn) {
return function functionC(params) {
var customparams = [params, {
something: "else"
}];
return fn.apply(null, customparams);
};
}
var load = functionA(obj.functionB.bind(obj));
load("a", "b"); // Works
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
2) By adding a second argument to functionA
, as Bergi suggested, that it uses when calling fn
(see in the snippet) — this approach is used by many of the ES5 Array
methods, for instance:
var obj = {
name: "foo",
functionB: function() {
snippet.log(this.name);
}
};
function functionA(fn, thisArg) { // <=== Accepting thisArg
return function functionC(params) {
var customparams = [params, {
something: "else"
}];
return fn.apply(thisArg, customparams); // <=== Using it
};
}
var load = functionA(obj.functionB, obj); // <=== Passing it
load("a", "b"); // Works
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>