Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32148
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Empty function |
| Date | 2017-01-09 01:10 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <3015126.QJadu78ljV@PointedEars.de> (permalink) |
| References | <UPqdnRaDtOBAT-zFnZ2dnUU7-RHNnZ2d@westnet.com.au> <o4sppl$ub7$1@news.albasani.net> <l-ednZuM5IV2ne_FnZ2dnUU7-SXNnZ2d@westnet.com.au> <1552504.VLH7GnMWUR@PointedEars.de> <04OdnQ527cwRUu_FnZ2dnUU7-bXNnZ2d@westnet.com.au> |
Andrew Poulos wrote:
> On 9/01/2017 9:40 AM, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>>> […]
>>> var Drag = {
>>> obj: null,
>>> init: function(o, posX, posY, minX, minY) {
>>> if (!o) return;
>>> o.onmousedown = Drag.start;
>>> o.root = o;
>>> o.root.style.left = posX + "px";
>>> o.root.style.top = posY + "px";
>>> o.minX = minX;
>>> o.minY = minY;
>>> o.root.onDragStart = function() {};
>>> o.root.onDragEnd = function() {};
>>> o.root.onDrag = function() {};
>>
>> As indicated by “o.root.style”, “o.root” appears to store a reference to
>> a DOM element object. In that case, the latter three lines are
>> error-prone:
>> […]
>>> },
>>> start: function(e) {
>>> e = e || window.event;
>>> var o = Drag.obj = this,
>>> y = parseInt(o.root.style.top, 10),
>>> x = parseInt(o.root.style.left, 10);
>>> o.root.onDragStart(x, y);
>>
>> Given the augmented code, this line suggests that the author attempted to
>> augment a host object (see above), and that they have no clue how to
>> properly dispatch an event to such an object.
>
> Thanks, I've stripped the references to onDragStart..., changed the
> event handler properties to all lowercase and it "works" in IE9+,
> Chrome...
You’re welcome. But given that “o.root” refers to a host object to which
the Law of Demeter does not fully apply (because it cannot safely be
augmented with a method), there should be a method on the wrapper object
with which to set the relevant “on…” properties of the wrapped object to
retain the supposed functionality of the wrapper.
The problem here is that the current approach does not use a wrapping object
in the first place but attempts to modify the target host object directly
when init() is called. The code needs to be refactored to avoid that, e. g.
var my = Object.create(null);
my.Dragable = function (target) {
this.init(target);
};
Object.assign(my.Dragable.prototype, {
/* to allow the wrapper to be reused for a different target */
init: function (target) {
if (this.target) this.clearEventListeners();
this.target = target;
},
setEventListener: function (type, listener) {
/* simplified */
this.target["on" + type] = listener;
}
});
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | 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
Empty function Andrew Poulos <ap_prog@hotmail.com> - 2017-01-08 16:49 +1100
Re: Empty function Martin Honnen <martin.honnen@gmx.de> - 2017-01-08 08:29 +0100
Re: Empty function Andrew Poulos <ap_prog@hotmail.com> - 2017-01-08 20:05 +1100
Re: Empty function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-08 23:40 +0100
Re: Empty function Andrew Poulos <ap_prog@hotmail.com> - 2017-01-09 10:47 +1100
Re: Empty function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-09 01:10 +0100
Re: Empty function JJ <jj4public@vfemail.net> - 2017-01-08 20:53 +0700
Re: Empty function "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-08 10:02 -0800
Re: Empty function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-08 23:21 +0100
csiph-web