调用方法在callback
我有这个Try类:
function Try () { console.log('Start') this.Something( function() { console.log('asd') this.Some() }) } Try.prototype.Something = function (callback) { console.log('hi all') callback() } Try.prototype.Some = function () { console.log('dsa') }
但是当我尝试在callback部分中调用Some方法时,它给了我一个错误,说this.Some is not a function
。 this.Some is not a function
。 问题是什么? 我怎样才能解决这个问题?
this
范围在不同的function
里面是不同的,哪怕是内在的function
你需要保留this
self
的外在function,并做到这一点
function Try () { console.log('Start') var self = this; self.Something( function() { console.log('asd') self.Some(); }) }