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 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> References: Reply-To: Thomas 'PointedEars' Lahn 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 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.