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


Groups > comp.lang.javascript > #28843

Re: Overwriting existing fucntion

Path csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!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 Mon, 19 Oct 2015 19:49:59 +0200
Organization PointedEars Software (PES)
Lines 87
Message-ID <2283142.7jlluFck8S@PointedEars.de> (permalink)
References <CbCdnUFUDeXC3rnLnZ2dnUU7-amdnZ2d@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 1445277000 27430 eJwFwQkBwDAIA0BLfGGNHArFv4TdwVOzv0hkYLGMuexZqeZRXA9EH3ls9XFugCZWb6wGxvcDIskRNw== (19 Oct 2015 17:50:00 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Mon, 19 Oct 2015 17:50:00 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwNxsEBwCAIA8CVhEiAcRRk/xHae52BwvJN47axmZZa2n6vTkAjEqtOe+7jL7sAjynnP8EDySTWJKvnafYHXCkVsw==
Cancel-Lock sha1:UZ81ejjVcfhw1QZOzilmT2K2U2g=
X-NNTP-Posting-Host eJwFwYEBwCAIA7CXEG0Z50CV/09Ygs1FxSF4MJiTFl+ZJ4Ow3k8RGlam5H47TGxfdR/aGvoBDBARLA==
Xref csiph.com comp.lang.javascript:28843

Show key headers only | View raw


Andrew Poulos wrote:

> I'm using a 3rd party elearning product that produces web pages. The
> product allows me to add "custom" code to a page.
> 
> I need to "overwrite" one of the functions that the product produces
> with one of my own. Custom code seems to always get added before their
> code eg.
> 
> […]
> 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.

> How can I add my function before theirs and still have my one trigger?

You cannot.  But (towards which you should have asked, see “X–Y problem”) 
you can overwrite their function afterwards by assigning a value to the same 
identifier, provided that they have not made the corresponding property 
read-only:

  document.addEventListener("load", function () { action70 = … }, false);

If the property is read-only but still configurable, you can make it 
writable again before that:

  Object.defineProperty(global, "action70", {
    writable: true,
    enumerable: true,   // or false
    configurable: true  // or false
  });

Properties resulting from variable declarations should not be configurable 
in the first place, so in that case you do not have to worry about read-
onlyness.

Different to what Stefan suggested, you should not be using “window” to 
refer to the global object, but if you need to make it explicit, “this” in 
the global execution context.  For example

  /* global context */
  var global = this;

  /* any context */
  document.addEventListener("load",
    function () { global.action70 = … }, false);

(A reference to the global object can also be obtained in other ways.)

Also, the host object referred to by “window” might not implement an 
“addEventListener()” method; “document” is safer, “document.body” is 
probably safest, given the history of the “load” event, but might not yet be 
available at this time (before the “body” element has been parsed).  
Consider using the “defer” attribute.  On the other hand, you appear to be 
using HTML5, so this difference might not of importance to you.  However, as 
in theory the document can be loaded before loading of the window content 
has been completed, using “document” or “document.body” instead might speed 
things up a little.

Note that this approach depends on the definition in W3C UI Events 
(formerly, W3C DOM Level 3 Events) that event listeners are added and 
executed in source code order.  If this needs to work where 
addEventListener() is not available (IE < 9 or IE 9 in Compatibility Mode), 
using window.attachEvent() instead of .addEventListener() does not suffice 
as, among other peculiarities, .attachEvent() does not observe addition 
order.

-- 
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