PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

jquery/javascript missing ) after formal parameters

Hi there I have this code:

function getFullnameDetails(mainHeight,upperstyle,type=''){
    setTimeout(function(){fullnameCenter(mainHeight,upperstyle,type='')},2000);
}
function fullnameCenter(mainHeight,upperstyle,type=''){
    var distOfMainAndUpper = mainHeight - upperstyle;
    var mainHalfHeight = mainHeight/2;
    var imageHeight = jQuery("img[rel='fullname']").height(); //there is a delay
    var imageHalfHeight = imageHeight/2;
    var fromImageTopToMainHalf = mainHalfHeight - imageHeight;
    var position = imageHalfHeight+fromImageTopToMainHalf-distOfMainAndUpper;
    if(type == 'big'){ jQuery("#temp .test1").css("bottom",position+"px"); }
    else { jQuery(".test1").css("bottom",position+"px"); }


}

It says that I'm missing ) after formal parameters. This happens on this line:

function getFullnameDetails(mainHeight,upperstyle,type=''){ //IT HAPPENS HERE! :)
    setTimeout(function(){fullnameCenter(mainHeight,upperstyle,type='')},2000);
}

What am I doing wrong here.

Thanks in advance for the help :)

Upvotes: 3

Views: 10212

Answers (3)

Timothy Jones
Timothy Jones

Reputation: 22125

As other answers have pointed out, JavaScript doesn't support default values in the function parameters. However, as also pointed out in the other solutions, using an inline || won't work when falsey values are passed (eg null, 0, '', NaN, false). A better solution is to check to see if the argument is undefined:

type = typeof(type) !== 'undefined' ? type : '';

or, more generally:

argument_name = typeof(argument_name) !== 'undefined' ? argument_name : default_value;

Of course, this means that passing undefined to a function will mean that the default value is used instead - but that should be pretty uncommon.

Upvotes: 3

Dave L.
Dave L.

Reputation: 9781

Are you trying to use like a default parameter or something?

type=''

That is not valid -- you can't have an equal sign in the parameter list of a function like that.

Upvotes: 1

sje397
sje397

Reputation: 41812

Javascript does not support default function parameter values.

You can do things like this (but be wary of unintended 'falsey' values):

function getFullnameDetails(mainHeight,upperstyle,type){
  type = type || ''; // if type is 'falsey' (null, undefined, empty string) assign ''
  //...
}

Upvotes: 8

Related Questions