0

I'm doing some js/html5-canvas, where a js function acts out the animation/drawing of the symbol, based on html5-canvas and some global variables. The latter are written from an external js.

My function works, if I call the parent function with the variables directly, but when i instantiate with "var x = new myFunction(args,..,..,..)" and then call "x", it doesnt quite get the arguments.

I'm rather new to javascript, so i'm guessing it might be something rather simple, that I've done wrong, and I'm hoping you guys can help.

// JavaScript Document// JavaScript Document
// Global Vars
var alrum_lysniveau = 0;
var alrum_setlysniveau = 0;
var alrum_gbLys;
var alrum_on = false;
var alrum_off = false;

// Init function

function init() {

    function dimSymbol(idCanvas, idBtnOn, idBtnOff, idSliderDiv, idSliderRange, lysniveau, rum, lys) {
        // Code for dimSymbol, not included here.

    };

    var alrumDim = new dimSymbol("canvasAlrum", "butOnAlrum", "butOffAlrum", "sliderAlrum", "sliderRange", alrum_lysniveau, "Alrum", alrum_gbLys);
    alrumDim;
}


// Load af function LivingRoom, når siden er indlæst
window.addEventListener('load', init, false);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
MCSN
  • 3
  • 1
  • *and then call "x"* — Why would you call an object created by a constructor function? Typically you would call a method on it. `x` doesn't call anything. To call a function you have to put `()` after it, with any arguments you want inside them (except when you use `new` when the `()` become optional if there are no arguments, which isn't the case here because you aren't saying `new alrumDim`). – Quentin Jul 21 '16 at 12:24
  • Hi Quentin, thanks for commenting. Instead of answering the why's you put, ill elaborate what I'm trying to do here. I have this function, the "dimSymbol". It contains around 70 lines of code. I want to run that function multiple times in the same page, but with different arguments, without having to copy the same 70 lines of code each time. The alrumDim is supposed to be such an instance, and the call on the line below, "alrumDim;" is to execute it. – MCSN Jul 21 '16 at 13:46
  • "The alrumDim is supposed to be such an instance" — So it isn't a function. If you want to call `dimSymbol` multiple times, then call `dimSymbol` multiple times. – Quentin Jul 21 '16 at 13:48
  • ""alrumDim;" is to execute it." — As I said, that does nothing. To call a function you need `()` (and that isn't a function). – Quentin Jul 21 '16 at 13:48
  • That was my first approach, just doing : dimSymbol("canvasAlrum", "butOnAlrum", "butOffAlrum", "sliderAlrum", "sliderRange", alrum_lysniveau, "Alrum", alrum_gbLys); But the values of alrum_lysniveau and alrum_gbLys doesnt work, its like theyre not passed through. – MCSN Jul 21 '16 at 13:50
  • They will be passed through. Possibly they are not the values you think they are. Possibly you are running into the [classic misunderstanding of how objects work in JS](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example). Either way you need to supply a proper [MCVE] focusing on your actual problem. – Quentin Jul 21 '16 at 13:52

2 Answers2

0

If you are trying to have a variable that you can use whenever and wherever to call your dimSymbol function with the same parameters every time, you could put the call to dimSymbol inside of a new function which is stored in a variable.

var alrumDim = function() {
    return dimSymbol("canvasAlrum", "butOnAlrum", "butOffAlrum", "sliderAlrum", "sliderRange", alrum_lysniveau, "Alrum", alrum_gbLys);
};
alrumDim();
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
0

Ill post my reply here, as I could not link the image in the comment-track from Quentin.

I tried making an example. The code is this:

function testFunc(x, y) {
    var elem = document.getElementById(x);

    elem.innerHTML = "Test message " + y;
}

testFunc("testdisplay", alrum_lysniveau);

As seen in the picture, the value of variable alrum_lysniveau is 56 when entering the testFunc call, however the data output written to the html is 0.

I tried looking at the linked posts from Quentin, regarding the js closure, and as such I tried making a return for an anonymous function, but that doesnt change anything, that I can see.

[Test code] https://i.stack.imgur.com/FtwnD.png

MCSN
  • 3
  • 1