Reputation: 3797
I want to bind the 'this' context of the following code to the Collection
prototype object, but right now it refers to the window object instead.
So far I have tried to wrap the function definition as a IIFE, but this doesn't change the context.
My code
Mongo.Collection.prototype.bulk = (function(){
var context = this; <------- should refer to the prototype's context and not the object 'bulk'
return {
insert: function(documents, options) {
},
update: function() {
},
upsert: function() {
}
};
})();
How can this be accomplished?
Upvotes: 0
Views: 195
Reputation: 19609
RGraham's comment is the correct answer.
There's no need for the IIFE. You can just do a plain prototype.fn = function() { var context = this; }
But if (for whatever reason) you can't do that... then you can pass whatever you want to use as the context
as a parameter to the IIFE:
Mongo.Collection.prototype.bulk = (function(context){
return {
insert: function(documents, options) {
},
update: function() {
},
upsert: function() {
}
};
})(Mongo.Collection.prototype);
Upvotes: 1