My question involves the performance gain or loss when declaring and using functions with JavaScript. Suppose I have a function, foo()
, defined as such:
function foo(arg1) {
//a simple CSS change, such as changing background color
}
Where arg1
is a String used to identify an HTML element. foo()
is triggered on a mouseover event. The CSS change happens using jQuery, but I don't think that's important to my question. I would like to revert the CSS property back to default on a mouseout event. Would there be performance advantages of declaring another function, foo2()
, such as:
function foo2(arg1) {
//undo the CSS change
}
Or would it be better to redefine foo()
as follows, using a second argument as a switch:
function foo(arg1,arg2) {
if(arg2 == 'change') {
//make the CSS change for arg1
}else if(arg2 == 'revert') {
//revert the change for arg1
}
}
I am not concerned with the load time of the page. My goal is to minimize my amount of code, but without hampering the speed of the CSS change.
EDIT: arg1
will not strictly refer to one element. It can be used to identify a set of elements, such as a class name shared by <td>
's in a column. This is why the :hover
CSS selector will not do what I need to do.
EDIT: Again, for clarity, suppose I have a set of elements containing the class arg1
. I want ALL of the elements to experience the CSS change even when only ONE of the elements with that class name triggers the event.