3

I have a wrapper function where I use a variable dataObject. I have an action to trigger some outside functions within the wrapper function.

function wrapper() {
    var dataObject;
    var jsonPath = "dataObject[0]['Set1'][0]['Attribute1']";
    eval('outsideFunction(dataObject, jsonPath)');
}


function outsideFunction(dataObject, jsonPath) {
    dataObject[0]['Set1'][0]['Attribute1'] = 'asde';  //This sets the value to dataObject in the wapper
    var attrVal = '123';
    eval("jsonPath = attrVal");  //This doesn't set value to dataObject in the wrapper but in the local dataObject
}

Why is there a difference in the action of direct assign and assigning using eval?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Okky
  • 10,338
  • 15
  • 75
  • 122
  • As a side not .. Eval ia highly discouraged in js for reasons of security – user1428716 Jan 31 '14 at 06:07
  • 5
    how about `eval(jsonPath + " = attrVal");`? – Seth Battin Jan 31 '14 at 06:07
  • 1
    ^^^^ In your code, what you are evaluating is the expression `"dataObject[0]['Set1'][0]['Attribute1']" = '123'`, i.e. you are assigning a string value to another string value, which is not possible. However, if you concatenate `jsonPath` with the other string, the result will be the expression `dataObject[0]['Set1'][0]['Attribute1'] = '123'`. However, note that this is really horrible code. Have a look at this question instead: http://stackoverflow.com/q/13719593/218196 – Felix Kling Jan 31 '14 at 06:08
  • possible duplicate of [Dynamic deep setting for a JavaScript object](http://stackoverflow.com/questions/6842795/dynamic-deep-setting-for-a-javascript-object) – Bergi Jan 31 '14 at 08:50

1 Answers1

1

According to your structure of data[0]['Set1'][0]['Attribute1'], which can be written as data[0].Set1[0].Attribute1, here is code, but I think you don't quite understand how many sets you were asking for.

var wrapper, outsideFunction;

wrapper = function(){

  someOb = {};

  var data = [
    {
      Set1: [
        {
          Attribute1: null, // we will change null to 'asdf' below
        },
      ],
    },
  ];

  outsideFunction(data, someOb);

  console.log( someOb.newProp, someOb.dumb );
  // outputs 'hehehehe', undefined

};

outsideFunction = function(data, blah) {

  data[0].Set1[0].Attribute1 = 'asdf';

  //blah is a reference to someOb
  // we can change a part of blah and it will look at the reference and change someOb
  blah.newProp = 'hehehehe';

  // but if we do `blah =`, then we reference a new object
  // This won't affect  someOb, blah will just be a different object talking about something else
  blah = { dumb: false };

};

So, like I was saying, your data object is a numbered set, ( you have [0] ), then a named set (Set1), then a numbered set ( [0] ), I don't think you mean to nest so much.

numberedSet = [
  { 
    name: 'dan',
    likes: [
      'coding', 'girls', 'food',
    ],
  },
  { 
    name: 'Sreekesh',
  },
]

namedSet = {

  dan: {
    isPerson: true,
    likes: [
      'coding', 'girls', 'food',
    ],
  },

  Sreekesh: {
    isPerson: true,
    askedQuestion: function(){
      return true;
    },
  }

};

numberedSet[0].name == dan; // true
numberedSet[0].likes[1] == 'girls'; // true
namedSet.dan.isPerson == true; // true
namedSet.Sreekesh.askedQuestion(); // true
dansch
  • 6,059
  • 4
  • 43
  • 59