Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Overwriting existing fucntion Date: Tue, 20 Oct 2015 02:17:08 +0200 Organization: PointedEars Software (PES) Lines: 69 Message-ID: <1867226.5Pa8prDpYa@PointedEars.de> References: <2283142.7jlluFck8S@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1445300230 14423 eJwFwYEBwDAEBMCVgvdhHFr2HyF3bhR+F3TC13ft5CgDuXuU7uyeozVhf7AMt0UToxJAaD0OABBJ (20 Oct 2015 00:17:10 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 20 Oct 2015 00:17:10 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNwoERwCAIBLCVFHjEcRD4/Udoc4H69jrmcAPBfydJKSzVuCPejJzAagqzy174sdQ9d6D6wvI+rCoORT6GPBbA Cancel-Lock: sha1:CiJl/chqHxF+jnbee4ji6XpMcYA= X-NNTP-Posting-Host: eJwFwQkBwDAIA0BL5QkBOV0B/xJ2BwuJRw+EY7HKvVP5uuYcoGESm8jPue2FUzJUo91pquYPF+QQjQ== Xref: csiph.com comp.lang.javascript:28846 Andrew Poulos wrote: > On 20/10/2015 4:49 AM, Thomas 'PointedEars' Lahn wrote: >> Andrew Poulos wrote: >>> […] >>> function ap() { >>> function action70(){ >>> // my customised function >>> } >>> } >>> if (window.addEventListener) { >>> window.addEventListener("load", ap); >>> } >>> >>> to get my function to "overwrite" the existing one but only their one >>> ever gets triggered. >> >> This works insofar that it is proper executable code, but the event >> handler calls the event listener referred by “ap”, which declares a local >> function and then exits, so that the Function instance just created by >> the declaration is immediately available for garbage collection – >> functionally a no-op, it may even be optimized to function ap () {} or, >> further, the addEventListener() call may be optimized out. > > This is the bit that caught me out. "function action70(){" is a local > function even though a function with the same identifier exists in the ^^^^^^ > global space. ^^^^^^^^^^^^ Yes; in general: in the outer execution contexts. It is easier to see if you think of a function declaration as a variable declaration with initialization in disguise: function ap () { function action70 () { // my customised function } } is equivalent to var ap = function () { var action70 = function () { // my customised function }; }; except that the original code is executed before all statements in the same execution context that are not declarations (misleadingly called “hoisting” elsewhere), and in source code order with other declarations in the same context. You would not expect that “action70” or its value is available outside ap unless ap() returned the value, and the same is true for function declarations. > Thank you for such a comprehensive answer. If you ever published a book > on scripting web pages I, for one, would buy a copy. You are welcome. Duly noted. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.