How can I wrap a javascript object property in a function programmatically? -


assume have object:

{   prop: somefunc() } 

now want grab prop's value part: somefunc() , wrap in function , return value:

function(){   return somefunc() } 

how can programmatically ??

mabye this:

it returns function preassigned property , calls property's function later.

function getpropfn(obj) {      return function () {          return obj.prop();      }  }    var obj = { prop: function () { return 42; } },      fn42 = getpropfn(obj),      fn42a = obj.prop; // without fancy stuff.    document.write(fn42() + '<br>' + fn42a());


Comments