+1
Declined

Supported Javascript methods

geurtsd 4 years ago in iRidium Script updated 3 years ago 9

Dear support team,


Despite the fact that your training and reference material has grown and professionalised a lot over the time, would it bw possible to publish a list of supported methods of standard javascript? Or a javascript version that the script editor is based on so we can research it ourselves?


I find regularly that standard javascript methods do not work in iridium, which makes it cumbersome to troubleshoot if I did something wrong in my programming, or if the method s just not supported.....


one example: Array method "indexOf"


thanks in advance.


cheers

Answer

PINNED
Answer

Polifill for indexOf("item of array")
add this script in your project and you will be able to use indexOf(array) thats available in ECMA5


// Polifill, IndexOf
// ECMA-262, 5, 15.4.4.14
// http://es5.github.io/#x15.4.4.14

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(searchElement, fromIndex) {
    var k;

    if (this == null) {
      throw new TypeError('"this" is null or not defined');
    }

    var O = Object(this);

    var len = O.length >>> 0;

    if (len === 0) {
      return -1;
    }

    var n = +fromIndex || 0;

    if (Math.abs(n) === Infinity) {
      n = 0;
    }

    if (n >= len) {
      return -1;
    }


    k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

    while (k < len) {

      if (k in O && O[k] === searchElement) {
        return k;
      }
      k++;
    }
    return -1;
  };
}

then you can use it:

var arr = ["All Systems", "Floor 1", "Room 1", "Room 3", "Room 4"];

var a = arr.indexOf("Room 1");
IR.Log(a);   // 2


Declined

Hello


Here you can see the version of language used in iRidium

And here is how it looks if I try to use indexOf in iRidium

Please use the public resources to know the list of supported functions, it should be correctly. Thank you!

Hi Ekaterina,


thanks for pointing the version level out to me.. that helps a lot... stupid of me to have missed this....


As for the indexOf... your example is for a string object.... I was talking about the Array object....


however it does not matter as the spec reference clarifies it,


Array.indexOf only has been adopted as of ECMA edition 5.... not in edition 3... ;-)


thanks for the quick help...


cheers,


Davy

PINNED
Answer

Polifill for indexOf("item of array")
add this script in your project and you will be able to use indexOf(array) thats available in ECMA5


// Polifill, IndexOf
// ECMA-262, 5, 15.4.4.14
// http://es5.github.io/#x15.4.4.14

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(searchElement, fromIndex) {
    var k;

    if (this == null) {
      throw new TypeError('"this" is null or not defined');
    }

    var O = Object(this);

    var len = O.length >>> 0;

    if (len === 0) {
      return -1;
    }

    var n = +fromIndex || 0;

    if (Math.abs(n) === Infinity) {
      n = 0;
    }

    if (n >= len) {
      return -1;
    }


    k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

    while (k < len) {

      if (k in O && O[k] === searchElement) {
        return k;
      }
      k++;
    }
    return -1;
  };
}

then you can use it:

var arr = ["All Systems", "Floor 1", "Room 1", "Room 3", "Room 4"];

var a = arr.indexOf("Room 1");
IR.Log(a);   // 2


+1

Hi Ekaterina,


yes, thanks. Had allready done that after knowing what the correct verion was.


thanks a lot for following up. appreciate it.


PS: general remark:

For indepth overview of support of functions and alternatives to add functionality for lower versions, see:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference


it also gives above codesnips to add missing functions, etc to lower version JS.


cheers,


Davy

Hi,


I'm wondering if there is a reason behind the use of the ancient version of ECMAScript standard.

What stops the team to update it so that it supports new features of the language.


Regards

Crabicode,


Without wanting to answer in the name of the Iridium team, yet I can allready think of a few reasons....

biggest being the regression testing involved with switching over to a newer version.... If I was their teams manager, I would rather have them spend time and effort in new functionality, instead of regression testing existing functionality..... Certainly seen that the missing code \ functionality can easely be added by each programmer by the means of the prototype overwrite as Ekaterine sampled above.....


It all boils down to a matter of choice and priorities....


Let me know if you are lacking any functions of higher script language and don't know how to work with the prototype to add it to your script....


cheers,


Davy

Geurtsd,


We extensively use REST XML API, thus requiring us to invent a wheel with the current iRidium JS env.

Particularly, we need ways to fetch and track resolutions of XMLHTTPRequest promises.


Any suggestions?


Rgrds

Cabricode,


I assume you ar talking about following promises:


Use the technique of Polyfill as Ekaterina proposed. (you kinda upgrade the current JS with new functionality by doing so)

search for "polyfill promise javascript" in google and take it from there....

One of the first hits shows me following:

https://github.com/taylorhakes/promise-polyfill


potentially you can just load the provided JS in your Project and start working with it (maybe ---- LOTS of testing required).


I must say, its one of the more advanced missing in the JS indeed.... (that I have not used yet....).


Potentially the answer lies in a different approach???

hope I'm able to point you on your way....

regards,


Davy

+1

Davy rights)
We are considering the possibility of moving to a new version of the JS engine, but the most conservative estimate, we rubs on tests about 3-4 months, which is unacceptable at this stage of development platforms (