Last active
December 22, 2015 08:59
-
-
Save jamtat/6449223 to your computer and use it in GitHub Desktop.
A js prototype with support for multiple onchange handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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