设置'this'多着呢
Settings the context of 'this' in IIFE
我想将以下代码的'this'上下文绑定到Collection
原型对象,但现在它指的是窗口对象。
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() {
}
};
})();
如何做到这一点?
格雷厄姆的评论是正确的答案。不需要生命。你可以直接输入prototype.fn = function() { var context = this; }
但是如果(不管出于什么原因)你不能那样做……然后你可以传递任何你想使用的context
作为IIFE的参数:
Mongo.Collection.prototype.bulk = (function(context){
return {
insert: function(documents, options) {
},
update: function() {
},
upsert: function() {
}
};
})(Mongo.Collection.prototype);
相关文章:
- AJAX 请求没有在我的 React 组件中设置 this.state.data
- 杰辛特 |传递默认设置 |函数声明和“this”
- 如何为“this”设置文本修饰
- 如何在 jquery 上的设置间隔函数中添加“this”
- 如何保存从单击this.nameClass动态设置的本地存储
- 在JQuery中设置$(this).text的样式
- 如何在 JQuery 中使用 $(this) 来为这些图像设置动画
- JQuery 将输入框值设置为 this.value onclick
- 如何设置 var self = this;从函数外部
- 在评估 javascript 时设置 this 指针
- this.refs 是一个空对象,尽管应该设置它
- View.render() 中的“this”被设置为模型
- 使用this from函数在作用域上获取/设置角度变量
- 为什么可以't通过this指针设置prototype的值类型属性
- 为什么调用带括号的方法,例如(obj.func)(),仍然设置“this”
- 为什么在类中的函数中设置this.property会生成新属性,而获取this.property则使用类属性
- AngularJs——设置'this'对象在控制器中的$http调用
- IE8设置$(this).val()时堆栈溢出
- 为什么调用array.prototype. foreach .call()时将数组设置为THIS对象不能工作?
- JavaScript -使用apply来调用函数,但是'this'未被设置为作为第一个参数传递的对象