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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wootaek Choi

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

    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?
  • Fox

    #2
    Re: How to check if the same function name is already used in javascript



    Wootaek Choi wrote:[color=blue]
    >
    > 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?[/color]

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

    Comment

    • Andy Fish

      #3
      Re: How to check if the same function name is already used in javascript

      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

      "Fox" <fox@fxmahoney. com> wrote in message
      news:3F408F23.4 B9BB92A@fxmahon ey.com...[color=blue]
      >
      >
      > Wootaek Choi wrote:[color=green]
      > >
      > > 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[/color][/color]
      javascrpt files,[color=blue][color=green]
      > > 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?[/color]
      >
      > if(!debug)
      > {
      > function
      > debug()
      > {
      > // whatever your code is...
      > }
      > }[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: How to check if the same function name is already used in javascript

        Fox <fox@fxmahoney. com> writes:
        [color=blue]
        > if(!debug)
        > {
        > function
        > debug()
        > {
        > // whatever your code is...
        > }
        > }[/color]

        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
        };
        }
        ---
        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...