我有这个 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')
}
但是当我尝试在回调部分调用 Some 方法时,它给出了一个错误,提示 this.Some 不是函数
。问题是什么?我该如何解决这个问题?
最佳答案
this
的作用域在不同的 function
中是不同的,即使它是内部 function
你需要在self
中保留外部函数的this
并使其
function Try () {
console.log('Start')
var self = this;
self.Something( function() {
console.log('asd')
self.Some();
})
}
关于javascript - 在回调中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35410299/