1

So I have some javascript with the following (pseudo) structure. How do I set the this.last_updated variable of the parent function from the showUpdates function, without specifically referencing the name assignment (my_main_function).

var my_main_function = new main()

function main() {
 this.last_updated; 

 function showUpdates(data){
 //set this.last_updated=

 // do Stuff
 }
 this.updateMain(){
  $.ajax({
                 url:"/my_url/"
                 type:"POST",
                 datatype:"json",
                 data: {'last_updated':this.last_updated },
                 success : function(data) { showUpdates(data)},
                 error : function(xhr,errmsg,err) {
                 alert(xhr.status + ": " + xhr.responseText); },
    });
 }
}
7
  • even for pseudo code you should make sure that it is clean. Is main really surrounding showUpdates and updateMain, why do you write var this.main_updated, Is this.updateMain a function that is part of my_main_function and if called will to an ajax request? Commented Jun 21, 2014 at 8:24
  • I'll change the var name to make it more clear - as you will see updateMain calls the update function passing to the server the timestamp of the last update. Commented Jun 21, 2014 at 8:26
  • What do you mean with but there is no need for more than one main() ? You only have one main object and you wont do a new main another time? If so you would not need new main() at all. Commented Jun 21, 2014 at 9:08
  • I'm a little not used to javascript, I was under the impression that the purpose of a function being assignable to a var was so that you could have multiple instances i.e. var first_timer = new timer(); var second_timer = new timer(); I guess there is no type of function that is declared by default? Because if I only want one timer() - the having a name for both the var and the function seems very redundant. Commented Jun 21, 2014 at 9:14
  • 2
    See How to access the correct this / context inside a callback?. Commented Jun 21, 2014 at 9:28

1 Answer 1

1

Updated the code base one the comments:

There are two way of creating objects.

If you need to create the object multiple time you will do it like this:

var YourDefintinon = function() {
};

YourDefintinon.prototype.foo = function() {

};

obj1 = new YourDefintinon();
obj2 = new YourDefintinon();

obj1.foo();

If you only need it once in your code you can just do it like that:

var obj = {

};

obj.foo = function() {

};

foo();

So your would need the main only once your code would look like this:
Using Function.prototype.bind (and its polyfill for older browsers) to bind the showUpdates to the obj.

var main = {
  last_updated : null
};

function showUpdates(data){
  this.last_updated = data.update_time;
}

main.updateMain = function () {
  //<< bind showUpdates  to `this` and save the bound function in the local variabel showUpdates
  var showUpdates = showUpdates.bind(this); 

  $.ajax({
     url:"/my_url/"
     type:"POST",
     datatype:"json",
     data: {'last_updated':last_updated },
     success : showUpdates, //<< uses the showUpdates variable not the function
     error : function(xhr,errmsg,err) {
       alert(xhr.status + ": " + xhr.responseText);
     },
  });
};

As you don't want to make showUpdates accessible to others you could wrap the whole block into a function that is immediatly called:

var main = (function() {
  var main = {
    last_updated : null
  };

  function showUpdates(data){
    this.last_updated = data.update_time;
  }

  main.updateMain = function () {
    var showUpdates = showUpdates.bind(this); 

    $.ajax({
       url:"/my_url/"
       type:"POST",
       datatype:"json",
       data: {'last_updated':last_updated },
       success : showUpdates,
       error : function(xhr,errmsg,err) {
         alert(xhr.status + ": " + xhr.responseText);
       },
    });
  };

  return main;
}());
Sign up to request clarification or add additional context in comments.

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.