If else for literal

D

David

Can some sort of is else condition be used for the following to change the
onclick to onmouseover depending on an argument in the surrounding function?
All of the code in the .. code to execute ... is very long and exactly
the same for both onclick or onmouseover events. Otherwise I would just make
2 functions.


function doIt(argument){
element.onclick = function {
.. code to execute ...
};
}
-------------------------------------
Laymens terms, like this..

function doIt(argument){
if(argument == 1){
element.onclick = function {
}else{
element.onmouseover = function {
}
.. code to execute ...
};
}


David
 
R

Richard Cornford

David said:
Can some sort of is else condition be used for the following
to change the onclick to onmouseover depending on an argument
in the surrounding function? All of the code in the .. code
to execute ... is very long and exactly the same for both
onclick or onmouseover events. Otherwise I would just make 2 functions.


function doIt(argument){
element.onclick = function {
.. code to execute ...
};
}
-------------------------------------
Laymens terms, like this..

function doIt(argument){
if(argument == 1){
element.onclick = function {
}else{
element.onmouseover = function {
}
.. code to execute ...
};
}

You could do:-

element[((argument == 1)?'onclick':'onmouseover')] = function(){ ...

See:-
<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >

- but that is going to be pretty obscure source code, and a slightly
unexpected design to be suing the same function for one of either onclick
or onmouseover depending on a parameter.

Richard.
 
D

David

[..]
David Golightly said:
Ahh, carrying bad PHP habits into the browser space...

For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:
but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:
[..]

var userAction = function(e) {
// your code here
}

function doIt(argument) {
if (argument == 1) {
element.onclick = userAction;
} else {
element.onmouseover = userAction;
}
}

Thanks for the info. I may have to rethink the script because ( using your
example ) the argument for the "element" is inside of the userAction var.
The doIt() function has no knowledge of what the "element" is.

David
 
D

David

Richard Cornford said:
You could do:-

element[((argument == 1)?'onclick':'onmouseover')] = function(){ ...

See:-
<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >

- but that is going to be pretty obscure source code, and a slightly
unexpected design to be suing the same function for one of either onclick
or onmouseover depending on a parameter.

Richard.

That works. I had no idea you could put if else statements inside of the
brackets like that.

Thnkas, David
 
D

David

David Golightly said:
For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:

http://blogs.msdn.com/ie/archive/20...ns-part-3-javascript-code-inefficiencies.aspx

but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:


You know, I don't think this would apply in this case. The outer function is
called only once onload to initiate the script, and the inner function. So
this function and the nested function objects ( applied to specific id'd
anchor tags ) are already stored in memory. I don't think extra instances of
these function objetcs are created when the event happens?

David
 
D

David

you're creating exactly the kind of circular
reference referred to in the MSIE blog article - the one the IE team
decided wasn't worth fixing.
lol...

Finally, it's probably not good programming practice to just say "Oh,
I'll just do it like this because it's a one-off thing, no need to
worry about scalability." If I can get the same result using a more
scalable technique, I get in the habit of doing it that way. You
never know when you may need to scale in the future! Just my 2c,
however; you're welcome to develop your own technique.

This function will only be called once onload and that's it. It will never
be placed in a looping condition ( scary thought ), however I do agree with
you about good coding habits. I'm not a rebel trying to re-create new
techniques nor take the easy way out.

Thanks again,
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top