How to check if the same function name is already used in javascript

W

Wootaek Choi

Hi, everybody..

I try to make common debug function
that call in every js files used in a HTML..

but if same debug function name is defined in.... more than two javascrpt files,
it would cause trouble.

so, I want to check if the function name is used at first,
and then define debug function.
Is the anyway for this?
 
F

Fox

Wootaek said:
Hi, everybody..

I try to make common debug function
that call in every js files used in a HTML..

but if same debug function name is defined in.... more than two javascrpt files,
it would cause trouble.

so, I want to check if the function name is used at first,
and then define debug function.
Is the anyway for this?

if(!debug)
{
function
debug()
{
// whatever your code is...
}
}
 
A

Andy Fish

I don't think it would actually cause any harm if you defined the function
twice. In javascript that's just like assigning a variable the same value it
already has.

as a matter of good practice I would use fox's suggestion though
 
L

Lasse Reichstein Nielsen

Fox said:
if(!debug)
{
function
debug()
{
// whatever your code is...
}
}

if the browser follows the ECMA specification, then that function is
not a function declaration, but a function expression. The name is then
only visible inside the function.

A lot of browsers don't, but that's not an excuse :)

This will be safer:
---
if (!debug) {
var debug = function() {
//code
};
}
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top