Ok, I'm trying to create a new object this way:
var src = {a:'a', b:'b', c:'c'}; var out = {}; for(var prop in src){ Object.defineProperty(out, prop,{ get: function(){ return src[prop]; }, set: function(val){ src[prop]=val; } }) }
And get a bad result:
out = {a:'c', b:'c', c:'c'}
I know other ways to create this object, since:
for (var prop in src) { (function(prop) { Object.defineProperty(out, prop, { get: function() { return src[prop]; }, set: function(val) { src[prop] = val; } }) })(prop) }
or
Object.keys(src).map(function(prop){ Object.defineProperty(out, prop,{ get: function(){ return src[prop]; }, set: function(val){ src[prop]=val; } }) })
But I canβt understand why in the first method the string parameter "prop" will be sent by the function "defineProperty" by reference. Help me understand this. Sorry for the bad english.
javascript object set get
3y3 Dec 09 '12 at 13:08 2012-12-09 13:08
source share