As you notice at Ecma-262 Edition 5.1 website, there is no const keyword.
const keyword was introduced in ES2015 (ES6) to define a new variable.
Rhino 1.7R4 Const Feature Compatibility Chart
This chart clearly shows that Rhino Engine in vRO only provides a basic support for const. However, we will do some tests to validate it.
What is const?
const or Constants are block-scoped variables declared using the const keyword. The value of a constant can’t be changed through reassignment (i.e. by using the assignment operator), and it can’t be redeclared (i.e. through a variable declaration). However, if a constant is an object or array its properties or items can be updated or removed.
Typical Properties of a const
- Cannot be reassigned
- Block Scope
- It can be assign on the variable on declaration line.
- Primitive value.
- The property of a const object can be change but it cannot be change to reference to the new object
- The values inside the const array can be change, it can add new items to const arrays but it cannot reference to a new array.
- Re-declaring of a const variable inside different block scope is allowed.
- Cannot be Hoisted.
- Create only read only reference to value.
How does a vRO const differ?
Now, I have also realized that even if const is supported in vRO, it doesn’t behave exactly like the normal const. So, I have done some testing by executing the same code in vRO 8.3 and on the Mozilla website here and compared the results. Let’s see which use-case passed the test and which one failed.
Error: Assignment to constant variable [FAIL]
Name-sharing with f(), variable or other const in the same scope [PASS]
const needs to be initialized [FAIL]
const in objects and arrays [FAIL]
const in for-in loop [FAIL]
With all these outcomes, it’s clear that vRO JS Engine is a totally different entity and it has its own set of rules. We just have to find them. If you like this article, feel free to share and comment. See you on other posts.