Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get source of nested function ?

Richard A. DeVenezia
Guest
 
Posts: n/a
#1: Jul 20 '05
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
Guest
 
Posts: n/a
#2: Jul 20 '05

re: How to get source of nested function ?


"Richard A. DeVenezia" <radevenz@ix.netcom.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(extractFunction(foo.toString(),"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.'
Vjekoslav Begovic
Guest
 
Posts: n/a
#3: Jul 20 '05

re: How to get source of nested function ?


"Richard A. DeVenezia" <radevenz@ix.netcom.com> wrote in message
news:bj5jp5$ejq6e$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


Fox
Guest
 
Posts: n/a
#4: Jul 20 '05

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.
Richard A. DeVenezia
Guest
 
Posts: n/a
#5: Jul 20 '05

re: How to get source of nested function ?


"Fox" <fox@fxmahoney.com> wrote in message
news:3F56DC7F.2538CABE@fxmahoney.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.inner2) ?

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


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#6: Jul 20 '05

re: How to get source of nested function ?


"Richard A. DeVenezia" <radevenz@ix.netcom.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.inner2) ?[/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.'
Closed Thread