What is this snippet of JavaScript doing? -
i don't understand each line doing in next snippet of javascript. understand it's reading whether person pressed come in or escape. want understand line 2, 3, 6, 10, 16, , 18 doing.
var okcancelevents = function (selector, callbacks) { var ok = callbacks.ok || function () {}; var cancel = callbacks.cancel || function () {}; var events = {}; events['keyup '+selector+', keydown '+selector+', focusout '+selector] = function (evt) { if (evt.type === "keydown" && evt.which === 27) { // escape = cancel cancel.call(this, evt); } else if (evt.type === "keyup" && evt.which === 13) { // blur/return/enter = ok/submit if non-empty var value = string(evt.target.value || ""); if (value) ok.call(this, value, evt); else cancel.call(this, evt); } }; homecoming events; };
var ok = callbacks.ok || function () {};
if callbacks.ok
undefined, creates stub
function. http://en.wikipedia.org/wiki/method_stub , sets ok
store reference function or reference in callbacks.ok
var cancel = callbacks.cancel || function () {};
the same callbacks.cancel
events['keyup '+selector+', keydown '+selector+', focusout '+selector]
creates object events
(previous line) , populates attribute 'keyup '+selector+', keydown '+selector+', focusout '+selector
function.
cancel.call(this, evt);
calls function reference stored in cancel
variable, listed arguments. https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/function/call
ok.call(this, value, evt);
the same, function reference in ok
variable.
javascript
No comments:
Post a Comment