0

We have a module that allows users to create some scripts.

I would like to determine if user has used correct properties etc.

For example user has wrote:

var test = session;

I can do:

var something = eval("session");
if (!something){
    // Throw error here
}

But i'm facing issues with this:

var test = session.id; // session id property is "id123456"

I thought i can do:

var something = eval("session.id");
if (!something){
    // Throw error here
}

But it seems it really does this:

var something = eval("id123456");

Any suggestions how can i determine wheter property exist in this object, also property in property and further?

As i said, i'm not trying to determine some certain object for some certain property, the strcture can be:

session.id.toString().name ... and further

In addition, i have just tried this:

session.hasOwnProperty("id"); // returned false

session.id // returned "id123456"
Cheese
  • 4,164
  • 5
  • 32
  • 59
  • what is wrong with simply using `session[id][name]...`? I really don't see why eval would be needed here – Icepickle May 13 '17 at 10:10

1 Answers1

0

Try code below:

eval('var session = {id:"id1234"}');
var x = eval("session")
console.warn(eval("x.id"))
console.error(eval("x.id.toString()"))
console.log(x.id)
Yashar Aliabbasi
  • 2,404
  • 1
  • 20
  • 34
  • Yeah, okay, but how can i write the id as a string, not a predefined property in my JS? For example: session.id.toString().name - i need dynamic check – Cheese May 13 '17 at 10:01
  • I added code for you.This support `session.id.toString()` too ;) – Yashar Aliabbasi May 13 '17 at 10:03
  • when i do eval(session.id) it shows errors in console: id123456 does not exist It uses the property value for evaluation – Cheese May 13 '17 at 10:06
  • What do you want that eval to return? – trincot May 13 '17 at 10:07
  • I answered based on your question and developed some real example.But I don't know what is your purpose.There must be `session` variable out there to evaluate values attached to it – Yashar Aliabbasi May 13 '17 at 10:11