Friday, 16 November 2018

How can I make Javascript parameters default to something if not present?




I have this function:



this.checkButton = function (buttonName, element, expectedPresent, expectedEnabled) {
var enabledCheck = expectedEnabled ? "enabled" : "disabled";
it('Find a ' + buttonName + ' button and check it is ' + enabledCheck, function () {
expect(element.isPresent()).toBe(expectedPresent);
expect(element.isEnabled()).toBe(expectedEnabled);

});
}


Is there a way I can make this so that if the function is called without the last two parameters then those parameters will both default to true?


Answer



If I understood what you want.



Try to say in starting of function.




expectedPresent = (expectedPresent==undefined) ? true : expectedPresent;
expectedEnabled = (expectedEnabled==undefined) ? true : expectedEnabled;


I hope this helps.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...