简体   繁体   English

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

[英]How can I pass variables to a new object in javascript?

I wish to implement this lib : 我希望实现这个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. 在Java中,我可以轻松地将参数传递给构造函数,除非需要函数句柄。

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. 我的目标是初始化一个new计时器调用以创建新计时器。 I need 5 different timers. 我需要5个不同的计时器。 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. 我的主要问题是我无法将this.interval设置为(例如)动态变量x ,因为我不知道如何将值传递给构造函数。


My implementation was based on the "code" from the documentation below: 我的实现基于以下文档中的“代码”

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

I'm not too sure you have a grasp on JavaScript and how much it really differs from Java - they are not that similar. 我不太确定您对JavaScript有什么了解,它与Java的真正区别是多少-它们并不相似。

This: 这个:

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

Doesn't work in JavaScript. 在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. 这是将makeMoreMilkshakes结果分配给回调,而不是函数本身。 If that's intended, then never mind this bit. 如果这是故意的,那么请不要介意这一点。 But I think you want: 但我认为您想要:

    callback: makeMoreMilkshakes,

without the () instead. 不使用()代替。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM