Skip to main content
Participating Frequently
December 17, 2017
Answered

Dynamic path

  • December 17, 2017
  • 2 replies
  • 171 views

Hi, I have functions i have to re-use with differents objects, i don't want to write a function for each symbol as they use the same mecanisms, but i don't know how to use dynamic targeting and parameters, everything i tried did'nt worked

for example, i tried to pass the instance name in parameters of the function, it does not work :

this.auto.addEventListener("click", obj_function.bind(this, obj='auto'));

function obj_function.(obj) {

    this.obj.goToAndStop(); 

    this.obj.removeEventListener();

}

Thanks for your help

    This topic has been closed for replies.
    Correct answer Colin Holgate

    I hadn't thought about working that way before, and I'll check into whether it works like I think it would.

    Assuming it works, either of these might do better:

    this.auto.addEventListener("click", obj_function.bind(this, obj='auto'));

    function obj_function.(obj) {

        this[obj].goToAndStop();

        this[obj].removeEventListener("click",obj_function);

    }

    or:

    this.auto.addEventListener("click", obj_function.bind(obj=this.auto));

    function obj_function.(obj) {

        obj.goToAndStop();

        obj.removeEventListener("click",obj_function);

    }

    2 replies

    Colin Holgate
    Colin HolgateCorrect answer
    Inspiring
    December 17, 2017

    I hadn't thought about working that way before, and I'll check into whether it works like I think it would.

    Assuming it works, either of these might do better:

    this.auto.addEventListener("click", obj_function.bind(this, obj='auto'));

    function obj_function.(obj) {

        this[obj].goToAndStop();

        this[obj].removeEventListener("click",obj_function);

    }

    or:

    this.auto.addEventListener("click", obj_function.bind(obj=this.auto));

    function obj_function.(obj) {

        obj.goToAndStop();

        obj.removeEventListener("click",obj_function);

    }

    Gico62Author
    Participating Frequently
    December 17, 2017

    Thanks a lot, it works perfectly (  I tried the first solution).

    Best Regards

    Gilles

    this[obj]