The PROMOTIC system uses the
VBScript language for scripting since the first version since
1995.
The time goes on and it is now clear that this language is no longer very modern. It is supported by Microsoft software products, but not by other manufacturers (e.g.
InternetExplorer supports it but
Chrome,
Firefox,
Edge ... do not).
During that time, the
JavaScript language reached a very strong position and is now considered to be the standard of internet technologies. Therefore it is now also a
standard language in the PROMOTIC system (since version
Pm9.0.0 this language can be used everywhere in PROMOTIC instead of the
VBScript language).
The purpose of this chapter is to show that even if the
JavaScript language is considered to be more general (and therefore also more complex), for the PROMOTIC system purposes the written code seems simpler and in many cases easier to understand than the code written in the
VBScript language.
The syntax is often similar to the
VBScript language. There are some differences that will be described here.
The difference in writing capital letters
Both languages differ from the point of view of using capital letters in names of variables, properties and methods.
The
JavaScript language is
case sensitive. Therefore there is a difference e.g. between the name
Value and
value. The names of properties and methods must be written exactly as defined in the documentation.
The
VBScript language is
NOT case sensitive. The property named "
Value" can be written as
value,
VALUE etc. But it is also recommended to write the system names as defined in the documentation.
Default properties or methods
The
VBScript language supports the concept of "default properties" (or methods). Each object can have one property or method defined as default and therefore it is not necessary to write it in the script.
Example:
The
PmaData object has
Item method defined as default method. The same way a variable in
PmaData (
PmVar object) has a default property
Value. Therefore it is possible to use multiple ways to access the variable value
"Temperature1":
Example1:
VBScriptSelect and copy to clipboard
oData.Item("Temperature1").Value = 95
' or
oData("Temperature1").Value = 95
' or
oData.Item("Temperature1") = 95
' or
oData("Temperature1") = 95
The
JavaScript language does not support this approach and therefore it is necessary to write each property or method. It means that in this case it must be written:
Example2:
JavaScriptSelect and copy to clipboard
oData.Item("Temperature1").Value = 95;
The difference in statements separation
In the
VBScript language, each statement is located on an individual row (i.e. the statements are separated by rows).
In the
JavaScript language, each statement must be separated by semicolon and so it is possible to place multiple statements on a single row (even if it is not wise to exaggerate this approach).
Example:
Two statements of value assignment located on individual rows
VBScriptSelect and copy to clipboard
Dim a, b
a = 1
b = 2
Two statements can be placed to individual rows
JavaScriptSelect and copy to clipboard
var a = 1;
var b = 2;
a = 1; b = 2;
// but both can also be on a single row
The difference in calling methods
When calling methods, the
JavaScript language seems to be easier to use, because there are
always brackets written after the method name (regardless of whether the method returns the value or not).
Example:
VBScriptSelect and copy to clipboard
Pm.Debug "some text"
' there are no brackets here because there is no value to be returned
oCommMsg.Run
' again, there are no brackets here and the method has no parameters
data = Pm.FileTextRead("#data:file.txt", 1)
' there are brackets here because the returned value is important for us
JavaScriptSelect and copy to clipboard
Pm.Debug("some text");
// the brackets are used always
oCommMsg.Run();
// the brackets are written even if the method has no parameters
data = Pm.FileTextRead("#data:file.txt", 1);
// the brackets are written again, in this case the syntax is identical with the VBScript
Variable creation and initialization
In the
JavaScript language, it is easier to create a variable and assign a value there. It can be done by a single statement, whereas in the
VBScript language, two statements are needed. For the assignment, there is no diffrence whether a simple value or an object is assigned, whereas in the
VBScript language, the
Set statement must be used for assigning an object.
Example:
The
Dim statement creates variables, but no value can be assigned
VBScriptSelect and copy to clipboard
Dim nVal, oPanel
nVal = 1
Set oPanel = pMe.Pm("/Panel1")
The
var statement creates variables and can also assign values
JavaScriptSelect and copy to clipboard
var nVal = 1;
var oPanel = pMe.Pm("/Panel1");
Language operators
Both languages share many common operators, for example addition (
+), subtraction (
-), multiplication (
*), division (
/) etc. But the
JavaScript language has more operators that may come handy. For example, the bit shift (
<< and
>>), conditional expression (
a ? b : c), summation/subtraction of one (
++ and
--).
Especially practical is the simplification of value adding (e.g. into the variable in the
PmaData object):
Example:
Adding the value of 15 to the variable means repeating also the right portion of the statement expression.
VBScriptSelect and copy to clipboard
pMe.Pm("/Data/#vars/Temperature").Value = pMe.Pm("/Data/#vars/Temperature").Value + 15
Adding the value of 15 to the variable is much simpler thanks to the
+= expression, which does not mean "assign", but "add". There are similar statements for "subtract", "multiply", "divide" ...
JavaScriptSelect and copy to clipboard
pMe.Pm("/Data/#vars/Temperature").Value += 15;
See also
The JavaScript operators and syntax description.
Language functions
Both languages have a large set of functions (in the
VBScript language for example the functions
Now,
Abs ...). The
JavaScript language also has a great number of similar functions, but
it is not recommended to use them - it is better to use just the
Pm object methods.
Language data types
Both languages has the data types like "integer", "Real number", "text string", "Object". The main difference is that in the
JavaScript language has
different types for "Date" and "Array". Both these types are used in the PROMOTIC system very often, but the implementation style of these types in the
JavaScript language is not suitable for the purposes of the PROMOTIC system. The problem has been solved as follows:
-
for the "Date" type: In the
VBScript language, the date is "something like a real number" that is not clear after a brief examination, what year, month, day, hour, minute, second, millisecond. it means. For this purpose, there are auxiliary
VBScript functions, that can be used for translating this "real number" into an apparent year, month, etc.
Similar concept is used also for the
JavaScript language: All methods of the PROMOTIC system, that return a date, return "something like a real number". This value can then be transmitted to other methods and most importantly, the new
PmDateObject object can be used for decoding the year, month, day, etc.
-
for the "Array" type: The
VBScript language has the "Array" type (it can be created for example by the
Array function). The most commonly used are the 1- and 2-dimensional variants.
Similar concept is used also for
JavaScript language: The new
Pm.CreatePmArray method can be used for creating "Array" as a special
PmArray object, and all
PROMOTIC methods that should return "Array", return this object.
The IF condition
Both languages can create a condition by the
IF statement. But in the
JavaScript language, the syntax is different.
Example:
VBScriptSelect and copy to clipboard
If a > 1 Then
' ...
' ...
Else
' ...
' ...
End If
The condition is executed by the
if...else statements. If there are multiple statements in the conditions, then these are written in brackets
{...}.
JavaScriptSelect and copy to clipboard
if (a > 1)
{
// ...
// ...
}
else
{
// ...
// ...
}
The cycle statement FOR
Both languages has the
FOR statement that allows to go through a portion of code multiple times. In the
JavaScript language, this statement has a slitly more comlicated syntax, but it is much more general.
Example:
VBScriptSelect and copy to clipboard
For iRow = 0 To 100
' ...
' ...
Next
The
for statement has three parameters.
- 1st parameter (e.g. i=0) is the statement that is executed at the beginning of the cycle.
- 2nd parameter (e.g. i<100) is an expression that is tested at the end of the cycle for detecting whether to continue or not
- 3rd parameter (e.g. i++) is the statement that is executed at the end of the cycle in order to modify the cycle variable
If there are multiple statements to be repeated in the
for statement, then these are written in the brackets
{...}.
JavaScriptSelect and copy to clipboard
for (iRow = 0; iRow <= 100; iRow++)
{
// ...
// ...
}
Statement for manifold condition
Both languages has the statement for manifold condition, when a set of statements is executed only if a variable reaches a defined value.
Example:
VBScriptSelect and copy to clipboard
Select Case nVar
Case 1
sMsg = "OK"
Case 2
sMsg = "Warning"
Case 3
sMsg = "Alarm"
End Select
The
switch statement. The
break; is written after each statement set that terminates the conditions and jumps off the
switch statement.
JavaScriptSelect and copy to clipboard
switch (nVar)
{
case 1:
sMsg = "OK";
break;
case 2:
sMsg = "Warning";
break;
case 3:
sMsg = "Alarm";
break;
}