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

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -