javascript - extending Function -


i've been seeing syntax:

var module = {      func: function(value) {          code;      }.func2("value");  } 

cropping in various places, wondering how done. remember seeing article while back, can't find now. whatever try .func2("value") trows syntax errors.

for example, take @ sproutcore's intro templateview.

todos.statsview = sc.templateview.extend({   remainingbinding: 'todos.todolistcontroller.remaining',    displayremaining: function() {     var remaining = this.get('remaining');     return remaining + (remaining === 1 ? " item" : " items");   }.property('remaining').cacheable() }); 

seems useful tool give users when writing factories.

thanks.

you can add properties function's prototype , chain function definitions. example:

function.prototype.trigger = function(trigger) {      var setvalue = this;      return function(thevalue) {         alert(trigger + ' set ' + thevalue);         setvalue(thevalue);     };  };  var _value = 0;  var setvalue = function(thevalue) {      _value = thevalue;  }.trigger('value');  setvalue(25); 

when setvalue defined trigger function called within scope of anonymous function it's chained to, , setvalue assigned value returned trigger. in case we're alerting setvalue being called, opens cool possibilities.

pretty slick.


Comments

Popular posts from this blog

how to build hyperlink for query string in php -

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

queue - mq_receive: message too long -