I have a tree structure that contains css selectors, my goal is to take the value from the input element that matches the selector and put them into the tree. For example if there's a <input type="text/> with the id textbox and the value 67, I want to do this {selector: "#textbox"} -> {value: 67}.
I've got this working in all cases, except when the input type is date. In that case I want to convert the string to a date object. My code looks like this
val = sel.val();
let type = sel.attr("type");
if(type == "number")
{
val = Number(val);
}
else
if(type == "date")
{
val = Date(val);
}
//#1
ast["value"] = val;
//#2
delete ast.selector;
At point #1, when the input type is date, val is a Date object. At point #2, ast["value"]/ast.value is a Date object as well. However, printing the entire ast object shows ast["value"]/ast.value as a string. This string value propagates through the rest of the code and causes issues. This is a quote from the console showing this
Wed Jul 27 2016 10:14:19 GMT-0600 (MDT) eval.js:249 //ast["value"] at point #2
Object {selector: "this", value: "Wed Jul 27 2016 10:14:19 GMT-0600 (MDT)"} eval.js:250 //ast at point #2
If the date weren't getting implicitly converted, the output would be
Wed Jul 27 2016 10:14:19 GMT-0600 (MDT) eval.js:249 //ast["value"] at point #2
Object {selector: "this", value: Wed Jul 27 2016 10:14:19 GMT-0600 (MDT)} eval.js:250 //ast at point #2
I've tested this code in Chrome and Firefox, both yield the same result.
Date.prototype.toStringIIRC is implementation dependent, as long as you're constructing the Date from the platform's own string representation you should be fine.