This article is a continuation of the first part that is about javascript in SAP HANA XS. 
The examples of new opportunities will be discussed here, as well as standard options, that allows the article be interesting for the beginners. The difference between the first part is that here we reveal no code execution results. Why should we demonstrate it if something interesting worth a trying by ourselves, while the obvious things are understandable by itself?

Code, description, so on..

The first example is an opportunity to observe the object. This method allows to track any changes in the properties. The code will explain my point better than me:


var o = { p: 1 };
var temp='';

o.watch("p", function (what, oldVal,newVal) {
    temp = temp + "o." + what + " changed from " + oldVal + " to "+ newVal +"<br>";
    return newVal;
});

o.p = 2;
o.p = 3;
delete o.p;

o.p = 4;
o.unwatch('p');
o.p = 5;
$.response.setBody(temp);   
$.response.contentType = "text/html";

This example is an alternative function declaration. After all you couldn’t need it 😛 :


var a = "return "+ "x*function() {return y}()";
var multiply = new Function("x", "y", a);
$.response.setBody(JSON.stringify({test:multiply(2,4)}));
$.response.contentType = "application/json";

The field of view is not always obvious for javascript beginners. And this example perfectly demonstrates that:


var a = 1;
function b() {
    a=10;
    function a() {return 5};
}
b()
var g = a;
$.response.setBody(g);   
$.response.contentType = "text/html";

But here the <let> comes for help. Could you guess what the result will be here?


var m=0;
var x=1;
var y=2;
let (x = x+10, y = 12) {
  m=x+y;
}
m=m-(x+y);
$.response.setBody(m);   
$.response.contentType = "text/html";

In this example I compare the performance of the array elements filling level. What do you think will work faster, the standard method “push” or writing a value to a nonexistent index? It seems that push and [<name>.length] works the same way … And that is not entirely correct:


var res1=[];
function times() {
var a = [];
var time1=Date.now();
for (var i=1;i<10000000;i++){
    a.push(i);//ex1
 a[a.length]=I; //ex2
}
var time2 = Date.now();
return (time2-time1)/1000;
}
for (var i=0;i<10;i++){
 res1.push(times());
}
$.response.setBody(uneval(res1));   
$.response.contentType = "text/html";

[2.035, 1.918, 1.939, 1.935, 1.933, 1.924, 1.945, 1.94, 1.948, 1.934] – push

[1.595, 1.564, 1.571, 1.556, 1.57, 1.554, 1.567, 1.57, 1.57, 1.574]- length

Collections support the uniqueness and therefore sometimes could be more useful than arrays:


var tem = new Set([1,2,'3']);
$.response.setBody(tem.has('3'));   
$.response.contentType = "text/html";

Default values for functions:


function av(a=1) {return a};
$.response.setBody(av());   
$.response.contentType = "text/html";

When I attended the first job interview in my life, I had this question: “How do you change the values ​​of two variables without using a third one”

Then I used only two ways: math way and Boolean “or”. I did not know then that it is possible to do like that:


var foo=1,bar=2;
[ foo, bar ] = [ bar, foo ];
$.response.setBody(‘foo - ’+foo+’bar - ’+bar);   
$.response.contentType = "text/html";

Is there any difference between “in” and “of” in an iteration? Of course it is, and here’s an example:


var arry=[1,2];

arry.someprop = 123;
var t=[];
for(var x in arry) {  // for(var x of arry)
 t.push(x);
}
$.response.setBody(uneval(t));   
$.response.contentType = "text/html";

If you are interested in the problem of using the multiple parameters with the usual named parameters, here’s an example for you:


function mult(m, ...th) {
  return th.map(function (x) {
    return m * x;
  });
}
var arr = mult(4, 1, 2, 3);
$.response.setBody(uneval(arr));   
$.response.contentType = "text/html";

This is one of the options to define getter – it is nothing new, but for somebody it could be useful:


var o = {a: 7, get b() {return this.a + 1;},set b(a) {this.c=a}};

o.a=2;
o.b=1;
$.response.setBody(o.b);   
$.response.contentType = "text/html";

Function parameter is the object. And this is possible:


var names = function({n1:n1,n2:n2}) {
 return n1+' '+n2;
}
$.response.setBody(names({n1:1,n2:2}));   
$.response.contentType = "text/html";

What do you think about the result of this function?


var a=1;
var b = (254,function() {return a+1}());
$.response.setBody(b+' a- '+a);   
$.response.contentType = "text/html";

Some labels:


var a=1;
xxx: {
        a=2;
        break xxx;
 a=3;
}
$.response.setBody(a);   
$.response.contentType = "text/html";

It is not obvious for everyone that there is also finally, isn’t it?


var a=0;
try {
    a+=1;
}
catch(e) {
    a+=2;
}
finally {
    a+=3;
}
$.response.setBody(a);   
$.response.contentType = "text/html";

This example is a little quiz, because three lines of code are missed. The returned result is “a = 2”. We can’t assign the value directly to a variable. What kind of lines are here? What do you think? I would like to note immediately that there is no error. Neither parameters nor brackets <kill_my_mind> doesn’t have!


var a=0;
…
…
…
kill_my_mind;
kill_my_mind;
$.response.setBody(‘a=’+a);   
$.response.contentType = "text/html";

Сonclusion:

Thx for your time.

P.S. The number of likes and followers encourages authors to write more;)

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply