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?

6
  • Please, share what is JustGage as well Commented Sep 26, 2017 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? Commented Sep 26, 2017 at 16:16
  • its a javascript add-on to create gauges. see github.com/toorshia/justgage Commented Sep 26, 2017 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 but really you should follow @KarlReid's advice and just use a return value. Commented Sep 26, 2017 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? Commented Sep 26, 2017 at 16:20

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

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.