[Try to] use prototype.apply for class Graphics

Posted on
  • Working on a Pixl,js with firmware 2v04 .

    Sometimes parameter for function are stored in arrays and apply is used to convert array to parameter.

    var numbers = [0,3,5,9,4];
    console.log(Math.max.apply(null, numbers));
    // output is 9
    

    Try to use this for g.drawString()

    var p = ["Hello", 10, 10];
    g.clear();
    g.flip();
    g.setFont("4x6",2);
    g.drawString.apply(null,p);
    g.flip();
    

    Screen does not show "Hello".

    Is this the correct syntax to use it in class Graphics?

  • Is this the correct syntax to use it in class Graphics?

    Almos :) The first argument of apply and call is the this argument. It doesn't matter for Math.max because max doesn't care about knowing it's called via Math, but drawString needs to know which Graphics instance it's called on.

    If you do g.drawString.apply(g,p); you should be fine

  • Thanks :)

    So g.drawString() also can be called this way g["drawString"]("Hello",10,10); and g["drawString"].apply(g,p); works too.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

[Try to] use prototype.apply for class Graphics

Posted by Avatar for MaBe @MaBe

Actions