Skip to content

Instantly share code, notes, and snippets.

@jamtat
Last active December 22, 2015 08:59
Show Gist options
  • Save jamtat/6449223 to your computer and use it in GitHub Desktop.
Save jamtat/6449223 to your computer and use it in GitHub Desktop.
A js prototype with support for multiple onchange handlers
function Widget(el) {
}
Widget.prototype = {
_onchangehandlers: [],
_value: 0,
set onchange(handler) {
if(typeof handler === 'function') {
this._onchangehandlers.push(handler);
return this._onchangehandlers.length-1;
} else {
throw 'An onchange handler must be a function';
}
},
get onchange () {
return this._onchangehandlers;
},
_notify: function(e) {
var i = 0,
h = this._onchangehandlers,
l = h.length;
for(i;i<l;i++) {
h[i](e);
}
},
set value(val) {
//Check to make sure value is valid and within range, throw exception if not
this._value = val;
this._notify({
value: val
});
},
get value() {
return this._value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment