0

I have function like:

function test(param1, param2, param3, param4, param5, param6, param7, param8){
    if(param1 === undefined)
        param1 = "1";
    if(param2 === undefined)
        param2 = "2";
    etc........................
}

I want in the calling on the function to make something like test(param1=2, param6=7) then automatically all other will be undefined is there is a way to do that?? Thank you...

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Kero
  • 79
  • 2
  • 10

5 Answers5

0

You can pass dict object. In particular case the parameter can be even empty, but in this case check it like in your post

function test(param) {
    default_values {
        'a':"default_a"
    }
    values = default_values.concat(param);
}

test({});
test({
    'b':"value_b",
});

This is very common solution.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
speedingdeer
  • 1,236
  • 2
  • 16
  • 26
  • 1
    Please don't call this a "json object" : there is no JSON here. – Denys Séguret Jun 06 '13 at 10:45
  • Thank you for help, but i just want to put parameters required in calling function as {param1: "2", param6: "3"} And in the test function i will keep my undefined parameters in case i used the regular (undefined, undefined, 1,2,undefined, 2.....) – Kero Jun 06 '13 at 11:07
  • you can use test(required_a,required_b,optional_dict) or you can just check if the given map contains required paramters. JS is very flexible there are many sullution. find this one the best for you. – speedingdeer Jun 06 '13 at 11:14
  • @pejot All the parameters in function are optional for example and i have 8 parameters then if i want to put param5 = 3 and neglect others I should call like `(undefined, undefined, undefined, undefined, param5)` instead i want to write Test(Param5: X) just as simple as this, thank you – Kero Jun 06 '13 at 11:47
  • @Kero, I know, I just wanted to propose more elegant way where you can define also some default values. – speedingdeer Jun 06 '13 at 12:22
  • @Kero, let's say I just wanted to present you my favorite way because I claim the sollution fits to described in the question needs. – speedingdeer Jun 06 '13 at 12:55
0

The only way to provide default parameters in JavaScript is to check if they are undefined and then assign a default value as you already said in your question.

But this will not help if you want to pass an undefined value explicitly.

LZR
  • 948
  • 10
  • 28
  • I'm just trying to make better use of optional parameters instead of writing 6 undefined and 2 values i will just write parameter and it's value... – Kero Jun 06 '13 at 11:37
0
var test = function(param1, param2, param3){
    this.param1 = param1 || "default value";
    this.param2 = param2 || "default value";
    this.param3 = param3 || "default value";
}

this?

Reko
  • 98
  • 1
  • 9
0

EDIT

this here is blazing fast as it changes only what it needs.

function test(a){
 var p={p1:'1',p2:'2',p3:'3',p4:'4',p5:'5',p6:'6'}
 for(var b in a){p[b]=a[b]}return p;
} 
console.log(test({p1:'1000',p3:'2000'}));

this is one way to go

1.define your default params in defaulParams

2.pass to the test function a similar object containing the params

var
defaultParams={p1:'1',p2:'2',p3:'3',p4:'4',p5:'5',p6:'6'},
test=function(a){
 var myparams={}
 for(var b in defaultParams){
  myparams[b]=a[b]||defaultParams[b];
 }
 return myparams
}
console.log(test({p1:'1000',p3:'2000'}));

another way to write this if it does not work for you.

 function test(a){
  var defaultParams={p1:'1',p2:'2',p3:'3',p4:'4',p5:'5',p6:'6'},myparams={};
  for(var b in defaultParams){
   myparams[b]=a[b]||defaultParams[b];
  }
  return myparams
 }

so now to extecute the function:

var anewlistofparams=test({p1:'1000',p3:'2000'})

or

var ihavethisparams={p1:'1000',p3:'2000'};
var anewlistofparams=test(ihavethisparams);

.

.

wrong way ... don't do that ... BAD

var
test=function(){
 var defaultParams={p1:'1',p2:'2',p3:'3',p4:'4',p5:'5',p6:'6'},myparams={};
 for(var a=0,b=arguments.length;a<b;a++){
  myparams['p'+(a+1)]=(arguments[a]!=''?arguments[a]:defaultParams['p'+a]);
 }
 return myparams
}

console.log(test('1000','','2000','','',''));

also the array way is bad....

var
test=function(){
 var defaultParams={p1:'1',p2:'2',p3:'3',p4:'4',p5:'5',p6:'6'},myparams=[];
 for(var a=0,b=arguments.length;a<b;a++){
  myparams[a]=(arguments[a]!=''?arguments[a]:defaultParams['p'+a]);
 }
 return myparams
}

console.log(test('1000','','2000','','',''));

why??

because if u have may parameters u end up by sending something like this each time

test('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','1','','','','','','','3','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');

using object with it's param name

test({p2589:'2589'});
cocco
  • 16,442
  • 7
  • 62
  • 77
  • It doesn't work with me only it sends nothing to the function. – Kero Jun 06 '13 at 11:31
  • just tested on safari,chrome,ie,safari mobile... works for me.you get an error? – cocco Jun 06 '13 at 11:46
  • I got nonworking function it takes wrong parameters according I'm also using this function as separated JavaScript and I'm calling it from different file.I'm considering case that i can remove default parameters and only take what user will enter... else neglect with check availability function... – Kero Jun 06 '13 at 12:08
  • i wrote the function including the default params maybe u forgot some ',{}[]var' – cocco Jun 06 '13 at 12:21
  • in you question ur function is test(p1,p2,p3) but when u pass real values to the fundction than it's something like that test(1,3)... sonow how can the function know which paramater is 3? can't..so to tell your function also the name of the params u need to includ the name of the params like p1=1 p3=3.there is also another way by sending always all parameters. so test('1','','3'). but that is BAD.. – cocco Jun 06 '13 at 12:25
0

You can pass an object to your function as an argument; you can even use a constructor that gives you an object with default values, like...

var DefaultArguments = function() {
    this.hello = 'Hello'
    this.world = 'world'
};

var myFunction = function(object) {
    return object.hello + ' ' + object.world + '!';
};


var newArguments1 = new DefaultArguments();
newArguments1.hello = 'Goodbye';
alert(myFunction(newArguments1));

var newArguments2 = new DefaultArguments();
newArguments2.world = 'Mars';
alert(myFunction(newArguments2));

http://jsfiddle.net/5qNXL/