我们能否在所有javascript方法中定义“this”的值
Can we define the value of "this" in all javascript methods?
所以我有一个骨干视图,我有这个方法
inscription: function() {
var self = this;
navigator.geolocation.getCurrentPosition(function(position) {
//I need to use self here because the context of this function
//is not my Backbone View
如何在 getCurrentPosition 回调中使用它?
使用 _.bind - _.bind(fn, context)
在您的情况下:
var self = this;
//...
var somefn = _.bind(function(position) {
var self = this;
//this/self refer to outer this
}, self)
navigator.geolocation.getCurrentPosition(somefn)
相关文章:
- this.router在AngularJS 2中未定义
- React+Meteor:this.ops返回未定义
- 定义this.properties或objectName.properties的javascript JSON对象
- `当使用箭头函数时,“开发工具”中未定义“this”
- jQuery$(this)引用未定义
- this.defaultValue 返回 select 元素的未定义
- 定义CommonJS模块时,fn.call(this)与fn()的对比
- Alt flux action:_this.actions未定义,即使调用了this.generateActions
- $(this).attr({class:“activeTab”});不起作用,因为这是未定义的
- ember.js计算属性中this的值是如何定义的
- 为什么会出现错误$this没有定义
- $(this)抛出“;未定义的“;但这是按预期进行的
- 我们能否在所有javascript方法中定义“this”的值
- jQuery两个不同的选择器,但根据哪一个定义$(this)
- `expressJS路由处理程序中未定义this
- 在Route类中未定义this.controller
- 路由问题:尝试重路由时没有定义this.context.router
- 定义“this"并向函数发送参数
- 重新定义“this"在函数原型中并使用新参数运行它
- javascript:定义"this"在函数的上下文中