7

This code works in JS

<script>
     let object_1 = "value";
     window.console.log(eval("object_" + 1));
</script>

But I try to do it in LWC and I get the error that the variable is "undefined":

   . . .
@api object_1;
   . . .
connectedCallback() {
    this.object_1 = "test";
    window.console.log(" 1: " + eval("this.object_" + 1) + " 2: " + eval("object_" + 1));
}

And the error that gives me:

"afterRender threw an error in 'c:my_component' [object_1 is not defined]"

1 Answer 1

9

The syntax for the second eval() misses this keyword while referring object_1

connectedCallback() {
        this.object_1 = "test";
        console.log(" 1: " + eval("this.object_" + 1) + " 2: " + eval("this.object_" + 1));
    }

Playground Link : https://developer.salesforce.com/docs/component-library/tools/playground/MIpc94lRV/1/edit

5
  • It still does not work in chrome & mozilla, but in IE 11 the function interprets the string with a warning of: SEC7131: Security of a sandboxed iframe is potentially compromised by allowing script and same origin access. Can it be that security issues do not work in other browsers?
    – Alex M.
    Jun 5, 2019 at 10:41
  • Wiered. Works in playground though. Might worth raising case with SF to get clarification. I tried same in locker console and seems eval is fine. developer.salesforce.com/docs/component-library/tools/… Jun 5, 2019 at 10:42
  • Any idea why the "eval" parameter interprets it as "undefined"? Because in the same Sandbox I have another problem with the "loadstyle" function that in IE 11 works but in the rest of the browsers it does not.
    – Alex M.
    Jun 5, 2019 at 11:08
  • @AlexM. Try in some developer org... If you can replicate it Jun 5, 2019 at 12:21
  • I read that it can be by the Lightning Locker. @PranayJaiswal
    – Alex M.
    Jun 5, 2019 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .