How to get source of nested function ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard A. DeVenezia

    How to get source of nested function ?

    Hi:

    function foo () {
    var xyz = 123;
    function bar () {
    var abc = 456;
    }
    }

    I can alert (foo) to see the function source

    Is there an alert (<something>) that will show me only the source of
    function bar inside function foo ?

    Richard A. DeVenezia


  • Lasse Reichstein Nielsen

    #2
    Re: How to get source of nested function ?

    "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> writes:
    [color=blue]
    > function foo () {
    > var xyz = 123;
    > function bar () {
    > var abc = 456;
    > }
    > }
    >
    > I can alert (foo) to see the function source[/color]

    How it looks can be browser dependent.
    [color=blue]
    > Is there an alert (<something>) that will show me only the source of
    > function bar inside function foo ?[/color]

    There is no way to access the function itself. It is a local function
    that is only created when foo is called, and that doesn't leave that
    scope again.

    All you can do is to find the definition of bar in the text of
    foo.toString().

    Perhaps this:
    ---
    function extractFunction (text,name) {
    var re = new RegExp("\\n(\\s *)function\\s+" +'bar'+"\\s*\\( .*\\n"+
    "((?!\\1\\}).*\ \n)*\\1\\}");
    var match = text.match(re);
    if (match) { return match[0];}
    }

    alert(extractFu nction(foo.toSt ring(),"bar"));
    ---

    It is very primitive. It finds the string "function bar" (or whatever
    function name) first on a line, and then finds the next line-starting
    "}" that is indented the same as the function keyword.

    /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

    • Vjekoslav Begovic

      #3
      Re: How to get source of nested function ?

      "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> wrote in message
      news:bj5jp5$ejq 6e$1@ID-168040.news.uni-berlin.de...[color=blue]
      > Hi:
      >
      > function foo () {
      > var xyz = 123;
      > function bar () {
      > var abc = 456;
      > }
      > }
      >
      > I can alert (foo) to see the function source
      >
      > Is there an alert (<something>) that will show me only the source of
      > function bar inside function foo ?[/color]

      Well, if you add one line of code, you will be able to achieve what you
      want. Try this:

      function foo () {
      var xyz = 123;
      this.viewsource = bar; //this is the one
      function bar () {
      var abc = 456;
      }
      }
      var a=new foo()
      alert (a.viewsource)

      Tested in IE6, NS6, Opera 6, Mozilla 1.3


      Comment

      • Fox

        #4
        Re: How to get source of nested function ?



        "Richard A. DeVenezia" wrote:[color=blue]
        >
        > Hi:
        >
        > function foo () {
        > var xyz = 123;
        > function bar () {
        > var abc = 456;
        > }
        > }
        >
        > I can alert (foo) to see the function source
        >
        > Is there an alert (<something>) that will show me only the source of
        > function bar inside function foo ?
        >
        > Richard A. DeVenezia[/color]

        This works (as you've declared foo) in Netscape browsers:

        alert(foo.bar);

        Another instance where JScript is not like JavaScript.

        Comment

        • Richard A. DeVenezia

          #5
          Re: How to get source of nested function ?

          "Fox" <fox@fxmahoney. com> wrote in message
          news:3F56DC7F.2 538CABE@fxmahon ey.com...[color=blue]
          >
          >
          > "Richard A. DeVenezia" wrote:[color=green]
          > >
          > > Hi:
          > >
          > > function foo () {
          > > var xyz = 123;
          > > function bar () {
          > > var abc = 456;
          > > }
          > > }
          > >
          > > I can alert (foo) to see the function source
          > >
          > > Is there an alert (<something>) that will show me only the source of
          > > function bar inside function foo ?
          > >
          > > Richard A. DeVenezia[/color]
          >
          > This works (as you've declared foo) in Netscape browsers:
          >
          > alert(foo.bar);
          >
          > Another instance where JScript is not like JavaScript.[/color]

          functions are objects / objects are functions

          Does NS permitting foo.bar imply it implictly thisifies a function in a
          function and implicity (anonymously?) instantiates the function when
          invoked?
          Does ECMA script spec indicate foo.bar should work as in NS ?
          Or should I get out of the deep end of the pool...

          This is a way that 'works' in IE (and I presume NS), however to use the
          funtion _I_ would have to instantiate it first, and use news to get at inner
          function declarations, which is annoying. Does NS handle nesting > 1
          (outer.inner1.i nner2) ?

          function outer () {
          // outer
          this.innerOne = function () {
          // inner 1
          this.innerTwo = function () {
          // inner 2
          }
          }
          }

          alert (outer) // IE source
          alert ((new outer).innerOne ) // IE source
          alert ((new (new outer).innerOne ).innerTwo) // IE source

          alert (outer.innerOne ) // IE undefined
          alert (outer.innerOne .innerTwo) // IE error

          --
          Richard A. DeVenezia


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: How to get source of nested function ?

            "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> writes:
            [color=blue]
            > functions are objects / objects are functions[/color]

            No. Functions are objects. Not all objects are functions.
            [color=blue]
            > Does NS permitting foo.bar imply it implictly thisifies a function in a
            > function and implicity (anonymously?) instantiates the function when
            > invoked?[/color]

            I am not sure what you mean by "thisify"
            [color=blue]
            > Does ECMA script spec indicate foo.bar should work as in NS ?[/color]

            No.
            [color=blue]
            > This is a way that 'works' in IE (and I presume NS), however to use the
            > funtion _I_ would have to instantiate it first, and use news to get at inner
            > function declarations, which is annoying. Does NS handle nesting > 1
            > (outer.inner1.i nner2) ?[/color]

            Yes (just tested).
            [color=blue]
            > alert (outer) // IE source
            > alert ((new outer).innerOne ) // IE source
            > alert ((new (new outer).innerOne ).innerTwo) // IE source[/color]

            Normal constructor behavior.
            [color=blue]
            > alert (outer.innerOne ) // IE undefined
            > alert (outer.innerOne .innerTwo) // IE error[/color]

            IE doesn't allow you to access "local" functions that way. It is
            Netscape/Mozilla only.

            /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...