javascript - How would I 'wrap' an object and replace all of the functions in it? -
javascript - How would I 'wrap' an object and replace all of the functions in it? -
this question has reply here:
javascript closure within loops – simple practical example 23 answers is there way wrap javascript methods function? 3 answershere's want achieve:
var obj = { something: 1, somefunction: function () { homecoming 'hello'; } }; wrap(obj).somefunction('hello'); // should different obj.somefunction defined above wrap(obj).something = 2; // should still work basically, want 'wrap' object , replace functions in else.
here's how i've defined wrap function:
function hello (v) { homecoming 'hello ' + v; } function wrap (oldobj) { var newobj = {}; (var k in oldobj) object.defineproperty(newobj, k, { get: function () { if (typeof oldobj[k] === 'function') homecoming hello; homecoming oldobj[k]; }, set: function (val) { oldobj[k] = val; } }); homecoming newobj; } (hello illustration replacement function)
when seek utilize this, in obj gets replaced function (i.e obj.something, set 1, replaced function). can't seem find issues code. i've tried debugging , haven't found issue yet.
what might issue? how else can solve problem?
edit 1: don't want replace functions in object itself, want create exclusively new object.
javascript function object wrapper
Comments
Post a Comment