Friday, July 26, 2019

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...