Consider I have two widgets.
$.widget( "custom.car", {
options: {
name: ''
},
_create: function() {
this.options.name = 'car'
},
random: function( event ) {
console.log(this.options.name);
},
clear: function() { console.log('Clear'); }
});
$.widget( "custom.bike", {
options: {
name: ''
},
_create: function() {
this.options.name = 'bike'
},
random: function( event ) {
console.log(this.options.name);
},
clear: function() { console.log('Clear'); }
});
isCar and isBike is based on user input.
if(isCar) {
$( "div" ).car();
$( "div" ).car('random');
} else if(isBike) {
$( "div" ).bike();
$( "div" ).bike('random');
}
If I call the random method in some where place I'll write the if condition.
If any possible solution to avoid the if condition or any other solution.
Note: My question is correct or not?
Not really, you'll have to decide which method you want to call so you'll have to use an
if...else ifconstruct or at least a ternary operator.Here is an example that reduces the number of statements:
In this example I assume that if
isCarisfalseisBikeistruewhich is not exactly like your example.