0

I have a javascript function like this

function drawImageOnCanvas(canvas, texturePath, color, type, onCall, modelPath, scene, pX, pY, pZ, scale, transform) {}

Here onCall, modelPath, scene, pX, pY, pZ, scale, transform are optional parameters. How can i pass value only for scale. Is there any way to define parameter name and pass value?

2
  • 1
    Use an object instead of bunch of arguments... Commented Feb 13, 2017 at 5:39
  • Or if you feel you must have separate arguments rather than using an object, pass undefined for the arguments you're wanting to skip. Either way, the function will have to test which values it has received and act accordingly. Commented Feb 13, 2017 at 5:48

3 Answers 3

3

You can use an object as the function's parameter.

function drawImageOnCanvas(canvas, options){
    var scale = options.scale;
}

And in call site:

drawImageOnCanvas(canvas, {scale: 2});

Also to handle optional parameters, you can check their existence using an if or || or ?:.

var color = /*default color*/;
if (options.color){
    color = options.color;
}

Or

var color = options.color || /*default color*/;

Or

var color = options.color ? options.color : /*default color*/;

Note: If options contains parameters having false, 0, etc values then the methods above are not suitable anymore. For example, assume we have a parameter called isActive, then passing {isActive: false} will lead to the /*default isActive*/. To address this problem, you can use .hasOwnProperty or in.

var options = {scale: 2, isActive: false};

console.log('isActive' in options); // true
console.log(options.hasOwnProperty('isActive')); // true
console.log(options.isActive); // false
Sign up to request clarification or add additional context in comments.

Comments

1

Anytime you have a function with many optional parameters, a much better option is to use a single parameter for all the optional parameters as an object.

In ES6, this case be more easily accomplished using destructuring and default parameters.

function drawImageOnCanvas(canvas, {scale = 1} = {}) {
    ...
}

Then you can call the function like this:

drawImageOnCanvas(canvas, { scale: 2 });

function test({ x = 10 } = {}) {
  console.log(x);
}

test({ x: 100 }); // passing x
test({ y: 200 }); // not passing x
test();           // not passing anything still works

Comments

0

You create an array having .length equal to the number of expected parameters to function, set parameters passed to function as elements of an array, set index 10 of array to value for scale, use rest element to pass array of parameters to function, destructuring assignment within function to define named variables

function drawImageOnCanvas(canvas, texturePath, color, type, onCall
, modelPath, scene, pX, pY, pZ, scale, transform) {
      console.log(canvas, texturePath, color, type, scale);
}

var args = Array.of(document.createElement("canvas"), 1, 2, 3, ...Array(7));

args[10] = {scale:456};

drawImageOnCanvas(...args);

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.