0

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?

3
  • 1
    "I want to call posts() in refresh function." Cool. And what's your question? Commented Sep 2, 2013 at 9:42
  • 1
    call like "self.posts(data);" :D You also have to pass in Data. But I do not see data constructed anywhere. Commented 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? Commented Sep 2, 2013 at 9:47

2 Answers 2

2

There are two problems in your code:

  • self doesn't refer to the object with the property posts, i.e. not to the object you are returning from the function. You have var self = this; and this refers to window (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:

Sign up to request clarification or add additional context in comments.

Comments

0
refresh :   function(){
    this.posts();
}

JSFIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.