Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my own JavaScript functions have required parameters?

When I'm writing javascript in intellij idea, I can check the parameters of a function I'm calling and it will say something like

Optional arguments

I'm not sure if this is an intellij feature where it knows about the standard library or if it's a javascript feature that lets you mark parameters as optional/required. Either way, I'd like to learn how I can create my own functions with optional/required parameters. All I know is that optional seems to be the default because Intellij says all my function's parameters are optional.


From the answers I'm seeing, it sounds like this is an Intellij feature. But, now I'm wondering if Intellij provides a way for me to mark my own function arguments as optional/required.

like image 383
Daniel Kaplan Avatar asked Sep 11 '13 18:09

Daniel Kaplan


4 Answers

Using ES6 default function parameters, you can implement a Required FLAG, that will throw an error and stop execution, when a function with required parameters is called without providing them.

// set a getter to a custom global identifier (P_REQUIRED)
Object.defineProperty( window , 'P_REQUIRED'  , {
  get : ()=>{
    let err     = new Error('');
    let trace   = err.stack.split('\n');
    let msg     = '';
    for(let i=2;i<trace.length;i++)  msg+=trace[i]+'\n';
    throw 'Error : Missing required parameter\n' + msg; 
  },
  configurable : false
});


// declare a function with a mandatory parameter
function myFunc( myParam = P_REQUIRED ){
  /* some code here */
}

// call the function without the parameters
myFunc();
  
// Error is thrown!

Essentially, P_REQUIRED is assigned to the parameter by the interpreter, when no value has been specified in the function call. This triggers the getter handler, which simply prepares and throws a new Error.

It`s a clean and cross-browser solution, that will run in any modern environment (ES6 is required).

I actually packed it as a javascript module.
+info : https://github.com/colxi/P_REQUIRED

like image 189
colxi Avatar answered Oct 23 '22 01:10

colxi


If you are just concerned with the code completion bit then you can get that in the IntelliJ environment with the use of JSDoc comments. This does not affect your code, as others have pointed out you cannot specify to the JavaScript interpreter if a parameter is optional or not, but the editor will pick up the information from the comment and display it as you type. That way your own functions display similarly to the library functions.

like image 22
Vincent Ramdhanie Avatar answered Oct 22 '22 23:10

Vincent Ramdhanie


I have another idea. I found this on some article.

create a function called required();

function required(varName) {
    throw new Error(`${varName} is required. `);
}

And when a parameter is required , just use like this:

fnName(a=required("a"), b = required("b")){
    ....
    ...
}

Code example :

function required(varName) {
    throw new Error(`${varName} is required. `);
}

function sumOfTwoValues(a= required("a"), b = required("b")){
   return a+b
}

console.log("sumOfTwoValues(1,2): ", sumOfTwoValues(1,2));
console.log("sumOfTwoValues(1): ", sumOfTwoValues(1));
like image 32
NIKHIL C M Avatar answered Oct 23 '22 00:10

NIKHIL C M


function foo(value1, value2){
/**
* @param myParam1 [Required]
* @param myParam2
*/
  if(value1 == undefined){
    throw('Value1 is a required field for function foo');
  }
}
like image 36
VoronoiPotato Avatar answered Oct 23 '22 00:10

VoronoiPotato