0

I wish to implement this lib:

var timer = new Tock({
      this.callback:  callbackFunction,
      this.interval:  10,
      this.complete:  completeFunction,
});

However, I wish to be able to make dynamic calls:

var milkshake = my_milkshakes[next];  
var timer = new Tock({
      this.callback:  makeMoreMilkshakes(),  // (Call version 2)
      this.interval:  shakeTime,
      this.complete:  bringThemToTheYard 
});

function bringThemToTheYard(){


    return setNextCallbackFunction();
}

The problem here seems to be that I cannot reach outside the scope of the initialization. In Java, I can easily pass arguments to the constructor, except in the case when a function handle is expected.

By abstract I mean that the code is not reusable through reference, but this is admittedly loose terminology.

My goal is to initialize a single new timer call to create new timers. I need 5 different timers. Ideally, a single timer would be enough because the timers are started and stopped in linear time.

My main problem is that I cannot set (for example) this.interval to a dynamic variable x, because I do not know how to pass a value into the constructor.


My implementation was based on the "code" from the documentation below:

var timer = new Tock({
  countdown: true,
  interval: 10,
  callback: someCallbackFunction,
  complete: someCompleteFunction
});
11
  • 2
    1) Javascript has no notion of "abstract constructors", it's unclear what you mean by that. 2) this.callback as a key of an object doesn't make a whole lot of sense, what's that supposed to be? 3) It's unclear what you're trying to achieve. Commented Jan 4, 2016 at 14:12
  • 2
    ">My goal is to initialize use a single new timer call to create new timers." So you need a kind of factory function? Commented Jan 4, 2016 at 14:20
  • 1
    "Abstract" in OOP typically has a very very very different meaning than "in place"; you better avoid the use of this word to reduce confusion. { this.callback: .. } is still a syntax error. Commented Jan 4, 2016 at 14:20
  • 1
    I'll just reiterate what @deceze said, it's hard to understand what your problem is exactly. Commented Jan 4, 2016 at 14:22
  • 3
    Regarding your edit: when you do new Tock({...}), you are passing a value to the constructor, namely an object literal containing various properties. But your object literal is malformed, because the keys can only be string literals, not this.something. Commented Jan 4, 2016 at 14:27

2 Answers 2

3

I'm not too sure you have a grasp on JavaScript and how much it really differs from Java - they are not that similar.

This:

var timer = new Tock({
    this.callback:  makeMoreMilkshakes(),  // (Call version 2)
    this.interval:  shakeTime,
    this.complete:  bringThemToTheYard 
});

Doesn't work in JavaScript. That's a syntax error as you can't use dot notation (.) on the left-hand side of an object definition. It seems like maybe you want something like:

function Tock(config) {
    var defaultCallback = callbackFunction;
    var defaultInterval = 10;
    var defaultComplete = completeFunction;

    this.callback = config.callback || defaultCallback;
    this.interval = config.interval || defaultInterval;
    this.complete = config.complete || defaultComplete;

    /**
     * This could also be written as follows, I just wanted
     * to emphasize how many people write "defaults" in JS
    this.callback = config.callback || callbackFunction;
    this.interval = config.interval || 10;
    this.complete = config.complete || completeFunction;
    */

    // Go on doing stuff....
}

var timer = new Tock({
    callback: makeMoreMilkshakes(),
    interval: shakeTime,
    complete: bringThemToTheYard
});

Also, this line looks off:

    callback: makeMoreMilkshakes(),

This is assigning the result of makeMoreMilkshakes to the callback, not the function itself. If that's intended, then never mind this bit. But I think you want:

    callback: makeMoreMilkshakes,

without the () instead.

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

Comments

0

The solution I was looking for was this:

function startNewTimer(type) {
  var timer = new Tock({});

  var options = null;

  switch (type){
    case "first":
      options.callback = function1;
      options.interval = interval1;
      options.complete = startNewTimer(second);
      break;
    case "second":
      options.callback = function2;
      options.interval = interval2;
      options.complete = startNewTimer(third);
      break;
    case "third":
      options.callback = function3;
      options.interval = interval3;
      options.complete = startNewTimer(fourth);
      break;
    case "fourth":
      options.callback = function4;
      options.interval = interval4;
      options.complete = startNewTimer(first); //Start over from function1
      break;
  }

  timer.callback = options.callback;
  timer.interval = options.interval;
  timer.complete = options.complete;

  timer.start(0);
  out("Starting timer: ".concat(timer.callback));
}

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.