1

I want to create some gauges, but my Gauge object remains undefined.

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

why? can't i pass variables to a function?

P.S.
  • 15,970
  • 14
  • 62
  • 86
David
  • 1,084
  • 12
  • 36
  • Please, share what is `JustGage` as well – P.S. Sep 26 '17 at 16:15
  • 4
    Primitives are passed by value, and `doesntwork` is `undefined` - a primitive. So if you modify it in the function as `name`, it has no effect on the `doesntwork` variable. Why don't you just return the result from `new JustGage` from the function? – Karl Reid Sep 26 '17 at 16:16
  • its a javascript add-on to create gauges. see https://github.com/toorshia/justgage – David Sep 26 '17 at 16:16
  • 1
    Indeed, one cannot pass *references to* variables to functions, you can only pass values. You could try [passing a string name](https://stackoverflow.com/q/1920867/1048572) but really you should follow @KarlReid's advice and just use a `return` value. – Bergi Sep 26 '17 at 16:19
  • 1
    @KarlReid: **Everything** is "pass by value". Only because objects are reference-type values doesn't mean they are "pass by reference". "Pass by reference" means this: `var a = {}; var b = a; b = {}; console.log(a === b); /*this is not true in JavaScript*/` . [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/q/373419/218196) – Felix Kling Sep 26 '17 at 16:20
  • @FelixKling that's good one.. – Sumeet Gohil Sep 26 '17 at 16:25

1 Answers1

5

No, you only pass values, not variable references or pointers.

For that simple example, returning would seem more appropriate.

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

However, I'm sure this is probably overly simplified. There are "reference types" in JS, so if works held an object, you could pass the value of the object reference and have the function populate a property of the object.

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);
llama
  • 2,535
  • 12
  • 11