Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
1,624

Introduction:

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 = g.next();
var temp2 = g.next();
var temp3 = g.next();
$.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 = o.next();
temp = temp+o.next();
temp = temp+o.next();
$.response.setBody(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:

3abc

Why 3abc, and not 12abc? Because o.next () 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:

аbc12

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) {
  res.push({k:key,v:value});
}
var temp = Maper.get(2)();

res.push({t:temp});
$.response.setBody(JSON.stringify({res:res}));   
$.response.contentType = "text/html";

The Result:

{"res":[{"k":0,"v":"zero"},{"k":1,"v":"one"},{"k":2},{"t":1}]}

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

res.push({k:key,v:value});

to:

res.push({k:key,v:uneval(value)});

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.setBody(a);   
$.response.contentType = "text/html";

Result:

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";

Conclusion

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 ...

1 Comment
Labels in this area