How can I create argument defaults for a function?
How can I create argument defaults for a function?
I have this function (the purpose of the lines I included were to set defaults if the user forget to input something simply to prevent an error. I realize there is some lack of logic here):
Code:
addHSLink = function (hsElem, hsUrl, hsType, hsTitle, hsWidth, hsHeight) { //hsElem[string], hsUrl[string], hsType[string], hsTitle[string], hsWidth[string/number], hsHeight[string/number]
var jQElem = $(hsElem);
if (typeof hsUrl !== 'string') {
hsUrl = 'http://someurl.com';
}
if (typeof hsTitle !== 'string') {
hsTitle = '';
}
if ((typeof hsWidth !== 'string' && isNaN(parseFloat(hsWidth))) || hsWidth !== 'number') {
hsWidth = 800;
}
if ((typeof hsHeight !== 'string' && isNaN(parseFloat(hsHeight))) || hsHeight !== 'number') {
hsHeight = 600;
}
//Some other code
}
I would like to save some programming space and time by finding another way to set defaults
I saw somewhere that you could set a default in the arguments list
Code:
function someFunction(aVariable = false) {
//some code
}
but when I try to do this with all my arguments, I get the error:
Quote:
"Missing ) after formal parameters"
Does that make sense