cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Variables declared with const are not block-scoped

SOLVED
Highlighted
Regular Member

Variables declared with const are not block-scoped

In ThingWorx 8.5.2-b90, variables declared with let are block-scoped, however, variables declared with const are not block-scoped.

 

Related section of the specification: https://tc39.es/ecma262/#sec-let-and-const-declarations

 

The let and const constructs were introduced in ES6. This article states that ThingWorx 8.5 supports ES6: https://community.ptc.com/t5/IoT-Tech-Tips/Arrow-functions-filter-map-amp-reduce-ThingWorx-8-5-suppo...

 

Related post: https://community.ptc.com/t5/ThingWorx-Developers/Incorrect-work-of-const-inside-while-loop/m-p/6638...

 

Related open issue on github: https://github.com/mozilla/rhino/issues/326

 

The following code snippet can be used to reproduce the issue:

  • test1a - no error using let
  • test1b - error thrown using const
  • test2a - no error using let
  • test2b is commented because it fails syntax checking

 

 

(function test1a() {
    var seen = {};
    var arr = ['a', 'b'];

    for (var i = 0; i < arr.length; i++) {
        let item = arr[i];
        if (seen[item]) {
            throw new Error('test1a: already seen: ' + item);
        } else {
            seen[item] = true;
        }
    }
})();

(function test1b() {
    // Same as test1a except variable `item` is declared using `const` instead of `let`
    var seen = {};
    var arr = ['a', 'b'];

    for (var i = 0; i < arr.length; i++) {
        const item = arr[i];
        if (seen[item]) {
            throw new Error('test1b: already seen: ' + item);
        } else {
            seen[item] = true;
        }
    }
})();

(function test2a() {
    {
        let a = 'a';
        if (a !== 'a') {
            throw new Error('test2a: a !== \'a\'');
        }
    }

    {
        let a = 'b';
        if (a !== 'b') {
            throw new Error('test2a: a !== \'b\'');
        }
    }
})();

//(function test2b() {
//    // Same as test2a except variable `a` is declared using `const` instead of `let`
//    {
//        const a = 'a';
//        if (a !== 'a') {
//            throw new Error('test2b: a !== \'a\'');
//        }
//    }
//
//    {
//        const a = 'b';
//        if (a !== 'b') {
//            throw new Error('test2b: a !== \'b\'');
//        }
//    }
//})();

 

 

Is this fixed in a later version of ThingWorx?

Tags (4)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Variables declared with const are not block-scoped

Hello,

 

The quick answer is "no": https://github.com/mozilla/rhino/issues/326

 

...unless PTC decides to switch away from Rhino, which I don't believe they have announced yet.

 

Regards,
Constantine

View solution in original post

1 REPLY 1
Highlighted

Re: Variables declared with const are not block-scoped

Hello,

 

The quick answer is "no": https://github.com/mozilla/rhino/issues/326

 

...unless PTC decides to switch away from Rhino, which I don't believe they have announced yet.

 

Regards,
Constantine

View solution in original post

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum!