How can i execute a function while creating an object using javascript
Running the following code
var y = [] ;
var x = [[1,2,3] , [4,5,6] , [7,8,9]]
for (var i = 0 ; i < 3 ; i++){
y.push({name :'MyName' , value:function(){return x[i]} }) ;
}
console.log(y) ;
will generate the following object
{name : 'MyName' , value : function (){return x[i]}
Question
how can i execute this object function having this :
{name : 'MyName' , value : function (){return [1,2,3]}
i tried eval()
it didn't work
Here is my jsFiddle
For the people who ask WHY
are you doing this ?
I'm doing this because object will be copied by value so i will end up having all me object.Value
equal to X[2] ;