N
nick
So, I was using the good old module pattern, and I had some generic
key handling code like this...
window.myApp = {}; // so this example will run
myApp.keys = (function(){
const down = 0, up = 1, callbacks = {};
var initted = false;
// public methods
return {
register : function (key, onPress, onRelease) {
if (!initted) init();
key = key === +key ? key :
(key+'_').toUpperCase().charCodeAt(0);
callbacks[key] = [onPress, onRelease];
},
unregister : function (key) { delete callbacks[key] }
}
// private methods
// ...
})();
.... and something hit me. You see that anonymous function calling
itself (closure) all the time. Wouldn't it look a lot cleaner like
this...
myApp.keys = new function(){
const down = 0, up = 1, callbacks = {};
var initted = false;
// public methods
this.register = function (key, onPress, onRelease) {
if (!initted) init();
key = key === +key ? key :
(key+'_').toUpperCase().charCodeAt(0);
callbacks[key] = [onPress, onRelease];
}
this.unregister = function (key) { delete callbacks[key] }
// private methods
function onKey(evt, fn) {
evt = evt || window.event; //IE reports window.event
var kc = evt.chrCode || evt.keyCode, cb = callbacks[kc];
if (cb && typeof cb[fn] == 'function') return cb[fn](kc);
return true;
}
function init(){
initted = true;
document.onkeydown = function(evt){ return onKey(evt, down) }
document.onkeyup = function(evt){ return onKey(evt, up) }
}
}
....I'm sure I'm not the first person who's thought of this, so I
assume there is some reason that it's wrong / bad / suboptimal or
you'd probably see it all over the place, but it sure looks a lot
neater. Maybe using new on an anonymous function with no argument list
is alright?
As an extension of that, what about calling throwaway closures with
new, maybe something like this?
delete new function { /* do stuff here; 'this' is a temp object. */ }
Thoughts?
key handling code like this...
window.myApp = {}; // so this example will run
myApp.keys = (function(){
const down = 0, up = 1, callbacks = {};
var initted = false;
// public methods
return {
register : function (key, onPress, onRelease) {
if (!initted) init();
key = key === +key ? key :
(key+'_').toUpperCase().charCodeAt(0);
callbacks[key] = [onPress, onRelease];
},
unregister : function (key) { delete callbacks[key] }
}
// private methods
// ...
})();
.... and something hit me. You see that anonymous function calling
itself (closure) all the time. Wouldn't it look a lot cleaner like
this...
myApp.keys = new function(){
const down = 0, up = 1, callbacks = {};
var initted = false;
// public methods
this.register = function (key, onPress, onRelease) {
if (!initted) init();
key = key === +key ? key :
(key+'_').toUpperCase().charCodeAt(0);
callbacks[key] = [onPress, onRelease];
}
this.unregister = function (key) { delete callbacks[key] }
// private methods
function onKey(evt, fn) {
evt = evt || window.event; //IE reports window.event
var kc = evt.chrCode || evt.keyCode, cb = callbacks[kc];
if (cb && typeof cb[fn] == 'function') return cb[fn](kc);
return true;
}
function init(){
initted = true;
document.onkeydown = function(evt){ return onKey(evt, down) }
document.onkeyup = function(evt){ return onKey(evt, up) }
}
}
....I'm sure I'm not the first person who's thought of this, so I
assume there is some reason that it's wrong / bad / suboptimal or
you'd probably see it all over the place, but it sure looks a lot
neater. Maybe using new on an anonymous function with no argument list
is alright?
As an extension of that, what about calling throwaway closures with
new, maybe something like this?
delete new function { /* do stuff here; 'this' is a temp object. */ }
Thoughts?