admin管理员组文章数量:1405564
This is my issue:
var greatapp = {
start : function(){
$.AJAX({
url : 'foo',
success : function(data){
greatapp.say(data);
}
})
},
say : function(s){
console.log(s);
}
}
What i don't like about this example is the fact that i repeat my self in the success
function by defining the object's name instead of just this
which obviously won't work because it's in an external function.
How to only have the name greatapp
one time in a JS object?
This is my issue:
var greatapp = {
start : function(){
$.AJAX({
url : 'foo.',
success : function(data){
greatapp.say(data);
}
})
},
say : function(s){
console.log(s);
}
}
What i don't like about this example is the fact that i repeat my self in the success
function by defining the object's name instead of just this
which obviously won't work because it's in an external function.
How to only have the name greatapp
one time in a JS object?
- See stackoverflow./questions/183702/… – Ivan Commented Jan 3, 2012 at 2:21
1 Answer
Reset to default 10A mon JavaScript idiom is to save the value of this
into a variable like me
or self
, and use that in the callback.
This will work since the callback has access to the variables declared in the enclosing scope—in other words the callback will form a closure over self
var greatapp = {
start : function(){
var self = this;
$.AJAX({
url : 'foo.',
success : function(data){
self.say(data);
}
})
},
say : function(s){
console.log(s);
}
}
本文标签: javascriptGet parent object within jQuery callbacksStack Overflow
版权声明:本文标题:javascript - Get parent object within jQuery callbacks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744332528a2601032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论