How to get source of nested function ?

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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
 
L

Lasse Reichstein Nielsen

Richard A. DeVenezia said:
function foo () {
var xyz = 123;
function bar () {
var abc = 456;
}
}

I can alert (foo) to see the function source

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

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
 
V

Vjekoslav Begovic

Richard A. DeVenezia said:
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 ?

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
 
F

Fox

Richard A. DeVenezia said:
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

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

alert(foo.bar);

Another instance where JScript is not like JavaScript.
 
R

Richard A. DeVenezia

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

alert(foo.bar);

Another instance where JScript is not like JavaScript.

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
 
L

Lasse Reichstein Nielsen

Richard A. DeVenezia said:
functions are objects / objects are functions

No. Functions are objects. Not all 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?

I am not sure what you mean by "thisify"
Does ECMA script spec indicate foo.bar should work as in NS ?
No.

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) ?

Yes (just tested).
alert (outer) // IE source
alert ((new outer).innerOne) // IE source
alert ((new (new outer).innerOne).innerTwo) // IE source

Normal constructor behavior.
alert (outer.innerOne) // IE undefined
alert (outer.innerOne.innerTwo) // IE error

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

/L
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top