473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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
Jul 20 '05 #1
5 6536
"Richard A. DeVenezia" <ra******@ix.netcom.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Richard A. DeVenezia" <ra******@ix.netcom.com> wrote in message
news:bj************@ID-168040.news.uni-berlin.de...
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
Jul 20 '05 #3
Fox


"Richard A. DeVenezia" wrote:

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.
Jul 20 '05 #4
"Fox" <fo*@fxmahoney.com> wrote in message
news:3F***************@fxmahoney.com...


"Richard A. DeVenezia" wrote:

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.


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
Jul 20 '05 #5
"Richard A. DeVenezia" <ra******@ix.netcom.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
7
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
0
by: tommaso.gastaldi | last post by:
I have a solution with 1 project on which I am working on say Pr1 I have another solution with another project, say Pr2. Source file in the existing Pr2 are organized within several nested...
4
by: Pyenos | last post by:
#!/usr/bin/python #################################################### # answers.py --- A simple answer bot # Copyright (C) 2006 Logan Lee # # MESSAGE: # # This program is a simple...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.