Path: csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Overriding function and adding a bit Date: Wed, 10 May 2017 19:22:38 +0200 Organization: PointedEars Software (PES) Lines: 62 Message-ID: <1565645.VLH7GnMWUR@PointedEars.de> References: <2811137.5fSG56mABF@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn NNTP-Posting-Host: 170.224.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: gwaiyur.mb-net.net 1494436960 7090 178.197.224.170 (10 May 2017 17:22:40 GMT) X-Complaints-To: abuse@open-news-network.org NNTP-Posting-Date: Wed, 10 May 2017 17:22:40 +0000 (UTC) User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Andrew Poulos wrote: >> function ObjLayerMoveTo(x, y) { >> /* 100+ lines of spaghetti code */ >> } >> >> […] >> var myObjLayerMoveTo = window.ObjLayerMoveTo; >> >> window.ObjLayerMoveTo = function(x, y) { >> if (this.id == "foo") return; >> myObjLayerMoveTo(x, y); >> }; >> >> >> Is this the way to approach this? > > No, it should be > > var myObjLayerMoveTo = ObjLayerMoveTo; > > ObjLayerMoveTo = function (x, y) { > if (this.id == "foo") return; > myObjLayerMoveTo(x, y); > }; > > Do not confuse the “window” object with the ECMAScript global object. Or, to keep the global namespace clean: /* in global context */ (function (global) { var _ObjLayerMoveTo = global.ObjLayerMoveTo; global.ObjLayerMoveTo = function (x, y) { if (this.id == "foo") return; _ObjLayerMoveTo(x, y); }; }(this)); or /* in global context */ ObjLayerMoveTo = (function (global) { var _ObjLayerMoveTo = global.ObjLayerMoveTo; return function (x, y) { if (this.id == "foo") return; _ObjLayerMoveTo(x, y); }; }(this)); I recommend either “_ObjLayerMoveTo” or “theirObjLayerMoveTo”, because “myObjectLayerMoveTo” would semantically be the function with which you override *their* “ObjLayerMoveTo”. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.