0
Отвечен

missing standard ECMAscript functions

r riksma 9 лет назад в iRidium Script / AV and Driver scripts обновлен Oksana (expert) 9 лет назад 6
I think it is always a good idea not to copy and paste code too much.
that's why i would like to create a function and use it many times over.

The problem with this is that the variable scope is defined when creating the function, not when running it.
for example:

var a = 456;
var func_b = function() {
IR.Log(a);
}
function func_a(param_a) {
var a = 123;
param_a();
}
func_a(func_b);

This returns 456 but i want it to return 123

To fix this there should be methods like call, apply and bind but they do not seem to exist in iRidium.

I also noticed Splice() does not work. (used to remove items from an array)

What is the current level of compatibility with the ECMA standard?
Is it at least possible to add the functions that are missing?

(extra resources:)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
http://javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/
Hello!


first of all we use ECMA 1.5. Function splice works fine. we test it =) your script does not works because you should use directive "this". Read about it on any javascript resource
Hello Xopyc,

I had some time to test these things again and found splice does work indeed. Must have been a typo or something, sorry about that.

My original code didn't work with the "this" directive (I did know about that one) but i refactored it and now it shouldn't need the other functions any more.

Next time i probably should leave the code a little longer and check again later before asking these questions.
Thanks for your help.

Hi,

When you use the following, then 123 is shown in the log.

var a = 456;

var func_b = function() {
IR.Log(a);
}

function func_a(param_a) {
a = 123;
param_a();
}

IR.AddListener(IR.EVENT_START,0,function()
{
func_a(func_b);
});
Thanks,


Roger
Ok, lets expand on this:

Your code modifies the original variable a
so the following code returns:

456
123
123



var a = 456;

var func_b = function() {
    IR.Log(this.a);
}

function func_a(param_a) {
    a = 123;
    param_a();
}

IR.AddListener(IR.EVENT_START,0,function()
{
  IR.Log(a);
  new func_a(func_b);
  IR.Log(a);
});



the next piece of code returns:

456
456
123
456



var a = 456;

var func_b = function() {
    IR.Log(this.a);
}

function func_a(param_a) {
    this.a = 123;
    param_a();
   
    IR.Log(this.a);
}

IR.AddListener(IR.EVENT_START,0,function()
{
  IR.Log(a);
  new func_a(func_b);
  IR.Log(a);
});


That's a little better but i would like it to return:

456
123
123
456

but  the "this" of func_b is the one of the window and not of func_a. that is what call and apply is for, to change the assignment of "this".

My previous example was a bit flawed because it lacked the "new" keyword.
I hope it makes sense what i'm after now.
For future reference, I solved my problem using this code:

var wheel = new Wheel();
var spokeWheel = new SpokeWheel();
IR.Log(wheel.rotates);
IR.Log(spokeWheel.rotates);
IR.Log(spokeWheel.hasSpokes);

function Wheel() {
    this.rotates = true;
}

function SpokeWheel() {
    Wheel.call(this);
    this.hasSpokes = true;
}


this returns "true" three times.

Сервис поддержки клиентов работает на платформе UserEcho