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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Javascript 1.7 in Thingworx Server-side script

jyoo1
1-Visitor

Javascript 1.7 in Thingworx Server-side script

Hello,

I'm trying to use server-side script more effectively, but I got some troubles on that.

First, As I have known, Thingworx Server-side script uses Rhino 1.7R4 which is based on Javascript 1.7. Then the script should support the functions of Javascript 1.7 such as "let".

But when I use the "let" statement in the script, it comes out "Sytanx Error". I think Rhino has no problem on that, then is this problem of javascript syntax checker of Thingworx?

Second, This is similar problem with above. If I use "for ~ in" statement on Array, then It makes the output weird.

Example codes is below.

---

1) Syntax Error of "let"

---

2) Error of "for each ~ in". it outputs unknown sentence starts with "_function".

If anybody knows this kind of problems and reasons, please reply. Thanks.

ACCEPTED SOLUTION

Accepted Solutions

On TW services there isn't much difference between var and let, as there "isn't" global scope ( it's just the service context which starts and ends on service calls ), then the difference between booth are minimum on a service scope.

To iterate arrays as far as I know:

// -- Calls myFunction(item,index) for each array item

myArray.forEach(myFunction);

// -- Iterates over each array item and sets item with it's value

for each(item in myArray) {}

// -- Iterates over each array item and sets index with the item's index on the array

for (index in myArray) {}

// -- Of course the "length" way

for (var i=0;i<myArray.length;i++) { }

* On all this samples the only one which can "simulate" let behaviour it's the first one, as all variables in myFunction will be local to myFunction, all the other ( the item, index and i samples ) you will have the variables defined out of for loop scope.

View solution in original post

4 REPLIES 4

On TW services there isn't much difference between var and let, as there "isn't" global scope ( it's just the service context which starts and ends on service calls ), then the difference between booth are minimum on a service scope.

To iterate arrays as far as I know:

// -- Calls myFunction(item,index) for each array item

myArray.forEach(myFunction);

// -- Iterates over each array item and sets item with it's value

for each(item in myArray) {}

// -- Iterates over each array item and sets index with the item's index on the array

for (index in myArray) {}

// -- Of course the "length" way

for (var i=0;i<myArray.length;i++) { }

* On all this samples the only one which can "simulate" let behaviour it's the first one, as all variables in myFunction will be local to myFunction, all the other ( the item, index and i samples ) you will have the variables defined out of for loop scope.

Thanks for your quick reply.

So as I understand, Thingworx script language doesn't follow ECMA-262 standard not completely. It has own customized version of Rhino. Am I right?

And for "for each" statement, I think it has a bug on that as I wrote at first.

If I log the length of the array lilke:

It makes the length of 'ar' as 5 which originally 4.

And If I output the elements of array.

It includes weird value on last index such as:

Hi,

I've tested your exact code and I get 4 not 5, I've tested it on 6.5.13 and 7.3.9, on which version are you testing it?

And yes it's a customized version of Rhino

I am using ThingWorx 8.0.1. Actually this morning I retry the situation but the bug has gone. Now it gets 4.

I'm not sure what is happening in my ThingWorx. Anyway thanks for your help!

Announcements


Top Tags