如何在javascript中将变量传递给新对象?

时间:2016-01-04 14:09:38

标签: javascript constructor abstract

我希望实施this lib

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

但是,我希望能够进行动态调用:

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();
}

这里的问题似乎是我无法达到初始化范围之外。在Java中,我可以很容易地将参数传递给构造函数,除非是在需要函数句柄的情况下。

通过抽象,我的意思是代码不能通过引用重复使用,但这无疑是松散的术语。

我的目标是初始化一个new计时器调用以创建新的计时器。我需要5个不同的计时器。理想情况下,单个计时器就足够了,因为计时器在线性时间内启动和停止。

我的主要问题是我无法将(例如)this.interval设置为动态变量x,因为我不知道如何将值传递给构造函数。

我的实施基于以下文档中的"code"

var timer = new Tock({
  countdown: true,
  interval: 10,
  callback: someCallbackFunction,
  complete: someCompleteFunction
});

2 个答案:

答案 0 :(得分:3)

我不太确定你是否掌握了JavaScript以及它与Java的真正区别 - 它们并不是那么相似。

此:

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

在JavaScript中不起作用。这是一个语法错误,因为您不能在对象定义的左侧使用点表示法(.)。看起来你可能想要这样的东西:

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
});

此外,这条线看起来:

    callback: makeMoreMilkshakes(),

这是将makeMoreMilkshakes结果分配给回调,而不是函数本身。如果这是有意的话,那就别担心了。但我想你想要:

    callback: makeMoreMilkshakes,

而不是()

答案 1 :(得分:0)

我正在寻找的解决方案是:

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));
}