javascript - 关于 this 获取当前对象父类的方法。
本文介绍了javascript - 关于 this 获取当前对象父类的方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
var o = {
a: function(){
console.log("a")
return this
},
b: function(){
console.log("b")
},
c: {
d: function(){
b() // 报错
console.log("c")
return this
}
}
}
如何在 d 中使用 b 方法?
解决方案
var o = {
a: function(){
console.log("a")
return this
},
b: function(){
console.log("b")
},
c: {
d: function(){
o.b() // 正确
console.log("c")
return this
}
//或者 d:function(){
// this.b()
//}.bind(o)
}
}
这篇关于javascript - 关于 this 获取当前对象父类的方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文