I want to call posts() in refresh function. Here is my code.
var TIMELINE = TIMELINE || (function (){
/*** private ***/
var _args = {};
var self = this;
return {
init : function(Args){
_args = Args;
}, // init
posts : function(data) {
alert('posts called');
}, // posts
unsetMarkers : function() {
alert('unsetMarkers called');
}, // unsetMarkers
refresh : function(){
self.posts;
}
};
}());
the problem is at this line self.posts;
I also tried self.posts({'data':'success','another':'thing'});
How I can use posts in refresh?
asked Sep 2, 2013 at 9:41
Bilal
2,6733 gold badges29 silver badges41 bronze badges
-
1"I want to call posts() in refresh function." Cool. And what's your question?Felix Kling– Felix Kling2013年09月02日 09:42:29 +00:00Commented Sep 2, 2013 at 9:42
-
1call like "self.posts(data);" :D You also have to pass in Data. But I do not see data constructed anywhere.Anand– Anand2013年09月02日 09:43:22 +00:00Commented Sep 2, 2013 at 9:43
-
@FlixKling the problem is at this line self.posts; I also tried self.posts(); How I can use posts in refresh?Bilal– Bilal2013年09月02日 09:47:20 +00:00Commented Sep 2, 2013 at 9:47
2 Answers 2
There are two problems in your code:
selfdoesn't refer to the object with the propertyposts, i.e. not to the object you are returning from the function. You havevar self = this;andthisrefers towindow(assuming non-strict mode).- You are not even trying to call the function.
Instead of returning the object immediately, assign it to self:
// instead of `var self = this;`
var self = {
// function definitions
};
return self;
and then you can call the method with
self.posts(); // note the parenthesis after the function name
If you are certain that the refresh function is always called as TIMELINE.refresh() (i.e. as a method of the TIMELINE object) , then you can also call the posts method with
this.posts();
and forget about self.
Further reading material:
- MDN -
this: Learn which valuethishas in different contexts. - Eloquent JavaScript - Functions: Learn how functions work.
answered Sep 2, 2013 at 9:45
Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
refresh : function(){
this.posts();
}
answered Sep 2, 2013 at 9:47
Anand
15k8 gold badges35 silver badges44 bronze badges
Comments
lang-js