var sampleFunction = function() {
var self = this;
console.log(self.somevalue);
console.log(?); //output : 2
};
sampleFunction().bind(this,2);
how to access this value 2 inside that samplefunction.
var sampleFunction = function() {
var self = this;
console.log(self.somevalue);
console.log(?); //output : 2
};
sampleFunction().bind(this,2);
how to access this value 2 inside that samplefunction.
You need to create a new function like so
var boundFunction = sampleFunction.bind(this);
boundFunction(2)
It's often easiest to use function.call like this answer
sampleFunction.call(this, 2);
If what you're trying to do is to create a new function that will always call sampleFunction
and pass it an argument of 2, then you would do that like this:
// define sampleFunction with one argument
var sampleFunction = function(data) {
console.log(data);
}
// create a new function that always calls sampleFunction
// with a value for this and a particular argument
var newFunc = sampleFunction.bind(this, 2);
// call that new function
newFunc(); // will put 2 in the console
I would also recommend reading the MDN doc page on .bind() so you can get a better description of what it does and how to use it.
Since you haven't provided any context for what you're really trying to do, you could also get the same result as above with either of these:
sampleFunction(2);
sampleFunction.call(this, 2);
If this is not what you're trying to do, then back way, way up and describe what you are actually trying to accomplish