我想创建一些仪表,但是我的仪表对象仍然未定义。

var doesntwork;
function create_gauge(name, id,  min,  max,  title,  label) {
  name = new JustGage({
    id: id,
    value: 0,
    min: min,
    max: max,
    donut: false,
    gaugeWidthScale: 0.3,
    counter: true,
    hideInnerShadow: true,
    title: title,
    label: label,
    decimals: 2
  });
}
create_gauge(doesntwork, "g2", 0, 100, "Füllstand", "%");
console.log(doesntwork); //undefined


为什么?我不能将变量传递给函数吗?

最佳答案

不,您只传递值,而不传递变量引用或指针。

对于该简单示例,返回似乎更合适。

var works;
function create_gauge(id,  min,  max,  title,  label) {
  return new JustGage({
    id: id,
    value: 0,
    min: min,
    max: max,
    donut: false,
    gaugeWidthScale: 0.3,
    counter: true,
    hideInnerShadow: true,
    title: title,
    label: label,
    decimals: 2
  });
}
works = create_gauge("g2", 0, 100, "Füllstand", "%");
console.log(works);




但是,我敢肯定这可能过于简化了。 JS中有“引用类型”,因此,如果works持有对象,则可以传递对象引用的值,并使函数填充对象的属性。

var works = {};
function create_gauge(obj, id,  min,  max,  title,  label) {
  obj.data = new JustGage({
    id: id,
    value: 0,
    min: min,
    max: max,
    donut: false,
    gaugeWidthScale: 0.3,
    counter: true,
    hideInnerShadow: true,
    title: title,
    label: label,
    decimals: 2
  });
}
create_gauge(works, "g2", 0, 100, "Füllstand", "%");
console.log(works.data);

09-11 20:30
查看更多