Subj : Re: outer visibility of nested functions To : netscape.public.mozilla.jseng From : =?ISO-8859-1?Q?Georg_Maa=DF?= Date : Sat Jan 10 2004 11:03 pm Brendan Eich wrote: > Georg Maaß wrote: >> js> function A() >> { >> function B() >> { >> print(this.name); >> } >> B.name = 'B'; > BTW, SpiderMonkey has predefined f.name for function f() {} since long > ago, so you don't need to do this if you're targeting only Mozilla or > Netscape. js> function A() { function B() {print(this.name);} } js> A.B(); A js> Yeah! I didn't know that. > Georg Maaß wrote: >> js> function A() >> { >> function B() >> { >> print(this.name); >> } >> B(); >> A.B = 'boom'; >> } >> js> A.B(); >> A >> js> A(); >> global >> js> A.B >> boah > > > Eh? Did you mistype, not copy-and-paste? Both. I replaced only one 'boah' with 'boom' instead of both. If the asginment is A.B = 'boom'; then requesting in js> A.B outputs boom not 'boah'. > Only in your example, only because A sets A.B = 'boom', and 'boom' is > not a function. This is a "do what I say" == "do what I mean" case, I > think. This is a very simplified sample. The old code was much more complex and worked well in times, when NN4 was much more in use than Mozilla. Later Mozilla change the implementation, which caused me to change the code because it no longer was comaptible to Mozilla. The original implementation used the nested function B to calculate a new function object and assign it to the outer function A as property B. The inner function B was absoulutely immune against any user manipulations like assigments to A.B. If the user assigned anything to A.B it did not harm the nested function B. So calling A allways created the function property A.B. Now with the extension the nested function B is no longer immune against destruction. If the user assigns something to A.B, calling A results in an error or a differnent behaviour when B is called, when the assigned stuff is also a function. This gives the user the power to override accidently or with criminal energy the intension of the implementor of A. > I don't see any point. More likely, if no one needs it, I'd remove the > old dependency (which exists only at compile time) of the outer function > object on the inner one being a named property of the outer. Hm. In this case switching requires at least 2 script blocks in a web page. The first switches the compilation behavior. The second block is then compiled with the new behaviour. Switching within one block might be possible, in case of creation using the Function constructor or perhabs function expressions. NN4 does not support nested functions within function expressions. So this would be probably not compatible with NN4. > See the > XXX comments at > http://lxr.mozilla.org/mozilla/source/js/src/jsparse.c#871 (among other > related XXX comments in the source). For performance reasons this may be the default. But to protect the inner function also the unpolluted tradition at least as option is desirable. This switching should be done in a way that does not harm NN4. > Are you targeting NN4 as well as more modern browsers with advanced JS? Yes, allways. I.e. on MacOS 9 and MacOS 8.x you can forget Mozilla it does not run stable and is extremly slow. It is no longer maintained, so there is no hope that it will ever run like known from Mozilla on Linux or Windows. I don't know whether Mozilla for MacOS X is better. At least it is maintained and therefor might become better. But old Machines live very long and do not support MacOS X. There may be similiar situations on other platforms with old machines. People become poorer and poorer. They can not effort allways new machnines just to run the newest browser. So support for ancient browsers is a must for a web page that is not intended as a rich people only page. An other problem when focusing on W3C-DOM is, that this is not supported by many browsers completely and in the same way. Browser sniffig exceeds much more, when using W3C-DOM then when using the classic DOM, which is mostly identical on nearby all browsers in the wild. -- Georg Maaß - bioshop.de D-76227 Karlsruhe, Westmarkstraße 82 HTML, XML / JavaScript, C++, Java, PHP, VB / CGI, JSP, ASP, ASP.net - The ultimate DHTML engine: http://gml-modul.sourceforge.net - http://sourceforge.net/projects/gml-modul .