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):
I would like to save some programming space and time by finding another way to set defaultsCode: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 saw somewhere that you could set a default in the arguments list
but when I try to do this with all my arguments, I get the error:Code:function someFunction(aVariable = false) { //some code }
Does that make sense"Missing ) after formal parameters"


Reply With Quote

Bookmarks