Skip to Content
Author's profile photo Former Member

Test yourself, or Do you really know JavaScript in SAP HANA XS?


In this article I would like to talk a bit about the possibilities xsjs which I did not find any documentation on scn, but which, in my opinion, would be useful to know those who study or work with SAP HANA XS about. The article is also dedicated to iterators, dynamic function arguments, easy way to copy objects, the Map object, a small example of currying, and an example of typed arrays.

Code, examples, explanation:

Definition of Iterator:

“An Iterator is an object that knows how to access items from a collection one at a time, while keeping track of its current position within that sequence. In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. “

For better understanding Iterator just have a look at the following code:

function Generator(){
  yield "num1";
  yield "num2";
  for (var i = 0; i < 3; i++)
    yield i;
var g = Generator();
var temp1 =;
var temp2 =;
var temp3 =;
$.response.setBody(temp1+', '+temp2+', '+temp3+' ');
$.response.contentType = "text/html";

The Result:

num1, num2, 0

The yield keyword is used to pause and resume a generator in a program.

Here is more complex example:

function vals() {
  for (var i = 0; i < arguments.length; i++) {
    yield arguments[i];
var o = vals(1, 2, 'abc'); 
var temp =;
temp =;
temp =;
$.response.contentType = "text/html";  

An iterator is also formed in this example. But the sequence number depends on the number of arguments in a function.

The Result:


Why 3abc, and not 12abc? Because () returns all the arguments one by one, but the variable temp first will be Number, and after retirning of the third argument –it will become  string.

And if the order of the arguments call is changed like this:

var o = vals('abc',1, 2); 

The result will be:


The following example shows a function that turns any object into a symbol representation. It is the Inverse function of eval – uneval.

One of the most obvious ways to use it is to copy objects:

var m = function () {this.a = 1};
var temp = new m();

temp.am_i_copy = 0;
var h = temp;
var h2 = eval(uneval(temp));
h2.am_i_copy = 1;
$.response.setBody(h2.am_i_copy+' - '+h.am_i_copy);   
$.response.contentType = "text/html";

The Result:

1 – 0

  1. i.e., h reference refers to object temp.

And Object h2 – the new one – is clone, so when we change property am_i_copy – to 1 source  h – doesn’t change. We couldn’t use JSON.stringify() and JSON.parse() because of functions..

The following example is a description of the Map() object. Map allows to operate the “key –value” pair. Here we create an array of values pairs ​​, and then we add to it the result of the method execution.

var res = [];
var Maper = new Map();
Maper.set(0, "zero");
Maper.set(1, "one");
Maper.set(2,function() {return 1;})
for (var [key, value] of Maper) {
var temp = Maper.get(2)();

$.response.contentType = "text/html";

The Result:


The important point is that the third object – {“k”:2} – doesn’t have value because of using function, so if we change this:




the result will be :

{“res”:[{“k”:0,”v”:”\”zero\””},{“k”:1,”v”:”\”one\””},{“k”:2,”v”:”(function () {\n\”use strict\”;\nreturn 1;})”},{“t”:1}]}

Next example will be interesting for people who doesn’t know about currying on javascript.

This is the example of javascript code which returns another function as the result of the first one and concatenate the first and the second values:

function curry_concat(x){
    return function(y){
        return x +'  '+ y;
var a = curry_concat('Test')('Curry');
$.response.contentType = "text/html";


Test Curry

Test yourself! Do you know the result of the following two statements? What do You think?

var x = new Uint8ClampedArray([-47]);
var x1 = new Uint8Array([-47]);
$.response.setBody('Clamped -' + x[0]+'; NoClamped'+x1[0]);   
$.response.contentType = "text/html";


Over the last example – if the community is interested in this topic, in the future I would be glad to write a detailed article about the pros and cons of using the typed arrays.

Besides the presented here functions there are also others that are not represented in the standard documentation. May be, it also will be interesting …

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Daniel Ruiz
      Daniel Ruiz


      I reckon you're looking for documentation in the wrong place. HANA XS uses SpiderMonkey, I'm not too sure what version it's embedded in your server, but should be 24 (JS 1.8.5) -- and since this is not an SAP 'technology', makes little to no sense looking into JS aspects in SDN.

      MDN (JavaScript reference - JavaScript | MDN) is a good reference, especially considering the engine XS uses.. you will find all that has been mentioned here and a bit more over there.