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


Groups > comp.lang.javascript > #28846

Re: Overwriting existing fucntion

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Overwriting existing fucntion
Date 2015-10-20 02:17 +0200
Organization PointedEars Software (PES)
Message-ID <1867226.5Pa8prDpYa@PointedEars.de> (permalink)
References <CbCdnUFUDeXC3rnLnZ2dnUU7-amdnZ2d@westnet.com.au> <2283142.7jlluFck8S@PointedEars.de> <R8ydnSViU8ql57jLnZ2dnUU7-K-dnZ2d@westnet.com.au>

Show all headers | 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