开发者

Evil Eval() not adding property to an object

I would like to 开发者_Go百科add property to an object with dynamical property name.

For this I'm using an eval(), but the compiler is returning : Unexpected token . Is what i'm doing possible, or eval() can't be used for objects like str vars

Here is a bit of my code

    var options = {}; // Creating empty object
$("#selAdresse, #selCP, #selVille, #selPays, #selNom, #selDescription").live('change', function(){
    var opt = eval("var options.lol"); // The evil eval();
    opt = "Test"; // Trying to set options.lol = "test"
    console.log(options); // Returning an empty object... Nothing change
});


You don't use var to add a property to an object. You use var to initially declare a variable:

var foo = {};

Adding properties is simple:

foo.bar = null;

If you have a string containing the property name, you use square bracket notation:

var propName = 'bar';
foo[propName] = null;

Edit: Based on your comment to kbok, square bracket notation is what you want:

var options = {};
options[ $( this ).attr( 'id' ) ] = 'Test';

End Edit

To be specific to your question (though you shouldn't be doing it and there are ways to avoid it), the syntax of your eval would look like:

eval( 'foo.bar = null;' );


Why not just do that ?

var options = {}; // Creating empty object
$("#selAdresse, #selCP, #selVille, #selPays, #selNom, #selDescription").live('change', function(){
    options.lol = "Test"; // Trying to set options.lol = "test"
    console.log(options); // Returning an empty object... Nothing change
});


The same behavior can be achieved using the following code:

var options = {};
options['lol'] = "Test"; // or options[$(this).attr('id')] = "Test"
console.log(options);


The problem is that you are treating object properties like their own variable. They work similarly, but this means you can't declare them. So when you say var options.lol, the interpreter is trying to parse options.lol as a valid identifier, which it obviously is not. In Javascript, you can just tack on properties dynamically, no need to declare them anywhere:

options.lol = 'Test';

Additionally, I assume the reason you are using an eval in the first place is because you don't know what property you will actually be setting. Javascript actually has a syntax for this:

options['lol'] = 'Test';

In reality, you should almost never need to use eval to do anything.


Looking at the comments & other answers it appears you are trying to get a reference to an object property? Javascript has no notion of getters and setters. You can't do this no matter what.

You can however define an empty object as a property, and create a reference to that.

  var options = {}; // Creating empty object

  options.lol={};
  opt = options.lol;

  // assign "text" sub-property of options.lol .. equiv. to options.lol.text="Test"
  opt.text = "Test";

You could also create a function that encapsulates the reference:

  // if falsy value is passed, then return current value. Will be limited because
  // you can't assign a falsy value.
  opt = function(text) {
      if (text) {
         options.lol=text;
      } else {
          return options.lol;
      }
  };

  opt("Test");
  alert(opt());

But there is absolutely no way to create a direct reference to a property in javascript, though, since objects just don't work that way internally. The property name itself is the reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜