Is there a way to create a dynamically object literal value in JavaScript? -
in other words, let value variable. this:
var a=3,b=4; var obj = {x : a, y : b}; alert(obj.x); //will display '3' a=2; alert(obj.x); //want display '2' - how can accomplish this?
make method:
var = 3, b = 4, obj = { x: function () { return a; }, y: b }; = 2;
and call like:
obj.x() // returns 2
demo: http://jsfiddle.net/67nwy/2/
otherwise there's no native (and supported in older browsers) way "live" value of a
using obj.x
. answer here provides use of object.defineproperty
can this.
you can apply obj.y
well, if wish same.
Comments
Post a Comment