Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28846

Re: Overwriting existing fucntion

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 <PointedEars@web.de>
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> (permalink)
References <CbCdnUFUDeXC3rnLnZ2dnUU7-amdnZ2d@westnet.com.au> <2283142.7jlluFck8S@PointedEars.de> <R8ydnSViU8ql57jLnZ2dnUU7-K-dnZ2d@westnet.com.au>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
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

Show key headers only | View raw


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: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Overwriting existing fucntion Andrew Poulos <ap_prog@hotmail.com> - 2015-10-19 12:12 +1100
  Re: Overwriting existing fucntion Andrew Poulos <ap_prog@hotmail.com> - 2015-10-19 13:57 +1100
  Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-19 05:04 +0200
    Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-19 13:14 +0200
  Re: Overwriting existing fucntion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-19 19:49 +0200
    Re: Overwriting existing fucntion Andrew Poulos <ap_prog@hotmail.com> - 2015-10-20 10:18 +1100
      Re: Overwriting existing fucntion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-20 02:17 +0200
    Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-21 19:47 +0200
      Re: Overwriting existing fucntion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-21 21:40 +0200
        Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-22 03:04 +0200
  Re: Overwriting existing fucntion Danny <dann90038@gmail.com> - 2015-10-19 13:29 -0700

csiph-web