473,756 Members | 5,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function declaration inside other function

Best explanation of my question will be an example, look below at this
simple function:

function SetEventHandler (element)
{
// some operations on element

element.onclick =
function(event)
{
// make something
}

}

Everything works fine, but I'm not sure about one thing - will the
onclick event handler be created in memory every time I call function
SetEventHandler , or browser will create only one instance and use it
for every element ? I can of course make that event handler as global
function and assign only reference to onclick event, but I'm just
wondering if there is any diffrence between these two methods.

Thanks for help

Dec 7 '06
28 4334
In comp.lang.javas cript message
<el************ *******@news.de mon.co.uk>, Sun, 10 Dec 2006 03:41:32,
Richard Cornford <Ri*****@litote s.demon.co.ukwr ote:
>No you don't, and until you appreciate ho
>w very little you do understand
about javascript and how utterly wrong most of your perceptions are, and
go back an start learning it over, you never will.

You're such a windbag that you really ought to have been able to find
time to produce a FAQ update within the last year or so; but that we
probably should be grateful that you did not in fact do so.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Dec 10 '06 #21
VK

Matt Kruse wrote:
<snip>
Richard, I think a diagram like above would help people to understand the
concept better, since many programmers are visual thinkers.
With some explanation of what is "visual thinker" in this case. I'd
like to avoid an impression that the "real programmers" (excluding
myself from this list - of course) are some low-IQ people: so the
matter gets on them only through Shockwave movies, blackboard drawings
and color balls and cubes :-)

This "visuality" is no much different from the common human mind
paradigm of making tools: first forming some mental model of the
desired tool - and then applying this model to the surrounding entities
in search of the most close to the model. Say if you need a piece of
wire to fix something, you first "see" the wire of the needed diameter
and length and then quickly looking around the garage: "too short...
not a wire... here!"

The only difference in programming that the "mental model" is some
programming entity you are working with or seeking to implement. The
"garage" then is paper-printed or Web resources you're quickly passing
over: "not my case... not that... here!"

Respectively an info source is bad not only if it's incomplete or if it
contains some wrong info. An info source is bad as well if it
"obfuscate" the model it describes up to the state of no match to the
"mental" model of the researcher: despite both models may be identical
or very close. Of course by carefully reading and studying the source
the researcher will find the match. But most of the time programmers
have no luxury to profoundly study some source which is "visually" not
what they after: with the only hope that some later deeper inside it
will be what they do need: instead "not my case... not that...
further".

In application to the discussed matter the quality of the current
information on the Web (from the criterion formed above) is sub-optimal
to say the least:

1) "DOM circular reference" (DCR) cases are illustrated solely and
exclusively by using closures. That makes people believe that unless
where is an inner function, DCR is not possible: "not my case...
further...".

2) MSDN graphs illustrating memory leak situations show such
complicated obfuscated relations that the first "visual" reaction
is "in my entire life I did not code such crazy sh** like that" :-)
So it hides the fact that the situation can be as primitive as two
lines of code one is using on the daily basis. There is a trend for
some reason to demonstrate not some real case - but an imaginary
situation brought "ad ultima Thule" (up to the extreme borders). The
sample you proposed to your friend is a bit of this kind IMHO.

3) Function expressions like:
function foo(elm) {
this.bar = function(){...}
// or
// elm.onclick = function(){...}
}

are taking ~99% "of the market" as opposed to ~1% inner function usage
like:
function foo(elm) {
this.bar = bar;
// or
// elm.onclick = bar;
function bar() {...}
}

yet any author seems feel self-obligated to illustrate closures only
with the second usage. It forms the impression that the first usage is
not really a closure - or some "harmless" closure (illustrated by
myself and by OP's question).

4) MSDN-specific: MSDN guys just cannot leave w/o that ActiveX. Even if
they are talking about something as universal as local vs global
variables: even then they still illustrate it with something like
var foo = new ActiveXObject(" ILove.ActiveX") ;
function Foobar() {
var foo = new ActiveXObject(" IReallyLove.Act iveX");
}

How much of very useful info was missed - being mistakenly taken as
ActiveX-specific - I'm scared to think about.

This way a really proper "visualizat ion" of a model has to be IMHO:
1) Illustrating one topic only at one time;
2) Illustrating exactly the topic spelled in the (sub)title;
3) Illustrating the topic on the most casual real life sample adequate
to the period of time the sample was made.

Dec 11 '06 #22
VK

Richard Cornford wrote:

<snip>
The next step in the demonstration is the code that can then be used by
others to reproduce the effects and make the pint for themselves (or
examined to expose flaws in the experimental process, so long as the
reader knows what it is that the code is supposed to demonstrate):-
< snip the code demonstating the default GC mechanics in JavaScript>

Eh... An excellent application of the Chewbacca defense: you won the
case, he's innocent. Can we come back now to the question of who did
kill the girl? (inner function's closure memory leaking)?

Despite I already provided enough of samples in this thread, let's make
it one more time in application to string values: using your suggested
*proper* schema of rationale, materials, tools, expected results and
the actual results. Some later today if you don't mind.
(in a technical newsgroup the "sapienti sat" condition can be presumed
in many circumstances. Thusly a perpetual "non sat" from the opponent
may put the question if he's "sapienti" enough for the current topic...
Just a side thought unrelated to the thread...)

Dec 11 '06 #23
VK wrote:
Respectively an info source is bad not only if it's incomplete or if
it contains some wrong info. An info source is bad as well if it
"obfuscate" the model it describes up to the state of no match to the
"mental" model of the researcher: despite both models may be identical
or very close.
I don't know what your native language is, and I can't fault you for
anything lost in translation, but sometimes I can't really understand what
you're talking about.
1) "DOM circular reference" (DCR) cases are illustrated solely and
exclusively by using closures.
No, they aren't. Look around. Closures just happen to be the most convenient
way to create the leak.
3) Function expressions like:
function foo(elm) {
this.bar = function(){...}
// or
// elm.onclick = function(){...}
}
are taking ~99% "of the market" as opposed to ~1% inner function usage
like:
function foo(elm) {
this.bar = bar;
// or
// elm.onclick = bar;
function bar() {...}
}
Do you really think there's much of a difference, here?
yet any author seems feel self-obligated to illustrate closures only
with the second usage.
Because it's easier to read. And for all practical purposes, identical to
the first useage.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 11 '06 #24
Dr J R Stockton <re*******@merl yn.demon.co.ukw rites:
In comp.lang.javas cript message
<el************ *******@news.de mon.co.uk>, Sun, 10 Dec 2006 03:41:32,
Richard Cornford <Ri*****@litote s.demon.co.ukwr ote:
[whatever]
You're such a windbag that you really ought to have been able to find
time to produce a FAQ update within the last year or so; but that we
probably should be grateful that you did not in fact do so.
As a regular lurker, I can’t help exclaiming that clj has to be the best
NG ever, having it all – lots of useful insight (Cornford, Ley, Winter,
Honnen, Kruse et al), classical harmless patronizing (Lahn), pure
entertainment (VK) and toffee-nosed upper class twits (Stockton).

Here’s an overdue “thank you”. :)
--
||| hexadecimal EBB
o-o decimal 3771
--oOo--( )--oOo-- octal 7273
205 goodbye binary 111010111011
Dec 13 '06 #25
VK

Eric B. Bednarz wrote:
pure entertainment (VK)
You mean memory leaks is nothing but an entertainment to you? Oh
well... whatever use - still use...

Dec 14 '06 #26
VK

Matt Kruse wrote:
An info source is bad as well if it
"obfuscate" the model it describes up to the state of no match to the
"mental" model of the researcher: despite both models may be identical
or very close.
I don't know what your native language is, and I can't fault you for
anything lost in translation, but sometimes I can't really understand what
you're talking about.
I provided some samples to explain it. Briefly and plainly: the source
is bad (IMHO) when the matter is illustrated with such convoluted and
far of real life example that this source will be skept as irrelevant:
despite this source is more than relevant.
That all was in continuation of your "visual thinkers" statement which
I am agree with.
1) "DOM circular reference" (DCR) cases are illustrated solely and
exclusively by using closures.
No, they aren't. Look around. Closures just happen to be the most convenient
way to create the leak.
You have then a reputable source where circular reference leaks are
illustrated as they are, without closures? Do you have a URL?
3) Function expressions like:
function foo(elm) {
this.bar = function(){...}
// or
// elm.onclick = function(){...}
}
are taking ~99% "of the market" as opposed to ~1% inner function usage
like:
function foo(elm) {
this.bar = bar;
// or
// elm.onclick = bar;
function bar() {...}
}
Do you really think there's much of a difference, here?
Why shoud anyone think otherwise? If only one kind of samples is
demonstrated: "*this* is closure, be aware of memory leaking" - then
another pattern may not trig the alarm when seen. "visual thinkers",
remember? Here is a good sample from another end:
<http://groups.google.c om.au/group/comp.lang.javas cript/msg/dce1775ff87cc18 6>
yet any author seems feel self-obligated to illustrate closures only
with the second usage.
Because it's easier to read. And for all practical purposes, identical to
the first useage.
How will anyone "see" it without parallel samples?

Dec 14 '06 #27
VK wrote:
>No, they aren't. Look around. Closures just happen to be the most
convenient way to create the leak.
You have then a reputable source where circular reference leaks are
illustrated as they are, without closures? Do you have a URL?
One of the most often-cited urls when faced with the memory leak issue:
http://msdn.microsoft.com/library/de...k_patterns.asp
http://tinyurl.com/dkyec
>>3) Function expressions like:
function foo(elm) {
this.bar = function(){...}
// or
// elm.onclick = function(){...}
}
are taking ~99% "of the market" as opposed to ~1% inner function
usage like:
function foo(elm) {
this.bar = bar;
// or
// elm.onclick = bar;
function bar() {...}
}
Do you really think there's much of a difference, here?
Why shoud anyone think otherwise?
Because they are pretty much identical. One creates a function by name in
the execution context and points to it, the other creates an anonymous
function in the execution context and points to it. Same diff.
If only one kind of samples is
demonstrated: "*this* is closure, be aware of memory leaking" - then
another pattern may not trig the alarm when seen.
But the above two examples don't even show a closure. Just inner functions.
>Because it's easier to read. And for all practical purposes,
identical to the first useage.
How will anyone "see" it without parallel samples?
You can give as many examples as you want. If it's all code, it's still not
"visual".

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 14 '06 #28
VK wrote:
Richard Cornford wrote:
<snip>
>The next step in the demonstration is the code that can then
be used by others to reproduce the effects and make the pint
for themselves (or examined to expose flaws in the experimental
process, so long as the reader knows what it is that the code
is supposed to demonstrate):-

< snip the code demonstating the default GC mechanics in JavaScript>
Are you suggesting that the reason that the memory increase of at least
2,000,000 MB that would be the logical consequence of creating 100,000
copies of a 20MB string is not observed because of garbage collection?
How does that work then, given that each string is assigned as the value
of an array element? In what way would garbage collecting the data
referred to by array elements while the array itself is still accessible
be "the default GC mechanics"? That sounds more like a serious bug to me
(or more likely another of your convenient fictions).
Eh... An excellent application of the Chewbacca defense: you
won the case, he's innocent. Can we come back now to the
question of who did kill the girl? (inner function's
closure memory leaking)?
You were the one concerned that the copying of large strings of data
would negatively impact on the issue, I have just demonstrated that you
were mistaken.

However, as you never made any actual statement bout "inner function's
closure memory leaking" beyond what was already well known what point
where you making?
Despite I already provided enough of samples in this thread,
Samples of javascript doing what javascript is specified as doing are of
no use in demonstrating anything without some expiation of what
specifically is being demonstrated and how it is being demonstrated.
let's make it one more time in application to string
values: using your suggested *proper* schema of
rationale, materials, tools, expected results and the
actual results.
From you I won't believe it until I see it.
Some later today if you don't mind.
<snip>

It seems to have been a very long day.

Richard.
Dec 16 '06 #29

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

Similar topics

1
4949
by: Dave Theese | last post by:
Consider the following declaration *inside of a function*: int j(int); My compiler (VC++ 7.1) accepts this. typeid returns a type of int __cdecl(int). Local functions are not legal in C++. Is this an error or... Is there some reason it would be legal to declare (but not define) a
2
3986
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class MyClass { // ... friend void MyFriendFunction (MyClass <T> * const ptrMyClass); // ...
2
8832
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book { public: Book()
4
15853
by: Brett | last post by:
I'm trying to use the F1 function inside of F2 function below. I keep getting the error posted below the code. If I remove the Shared declaration from F2, it works fine. What exactly does the error mean? Public Class myClass Function F1(ByVal url As String) As Struct1 -- do something -- End Function
3
3065
by: hugheslin | last post by:
Hi, Please consider the following classes: ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class Shape { ...... ...... public:
11
10993
by: rherring | last post by:
Hello, I have some C++ code that builds against 2 headers files that contain the same function name declaration (gethostname). The header files are not directly included. They are included via other header files. How can I resovle this error? The error (GCC 3.3.2 on Solaris 9): /usr/include/unistd.h:278: error: declaration of C function `int
20
2373
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
4
2719
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any function, it is global regardless of whether it's declared with or without "var" * When it is declared inside a function, if declared with "var", it's local, if not, it's global
4
2516
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse something like: int foo(int, int); const double *xpto(mytype *, mytype &) const; But I'm not being able to find my way around the grammar.
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.