简体   繁体   English

解决未捕获的SyntaxError:意外令牌>

[英]Resolve Uncaught SyntaxError: Unexpected token >

I'm have a function to transform text in a base64 image on my Cordova App. 我有一个在Cordova应用程序上转换base64图像中的文本的功能。 It's working fine but in a few devices there's an Unexpected token error on the script. 它工作正常,但在少数设备中,脚本上存在意外的令牌错误。

Here's the function: 功能如下:

    function socialShare(message, font) {
    var y = 12;
    var x = 18;
    var canvas = document.getElementById("receipt");
    var context = canvas.getContext("2d");

    // calcula a largura da string mais larga
    context.font = font;
    var maxStrWidth = message.map(e => {
        return context.measureText(e).width;
    }).sort((a, b) => {
        return b - a;
    });

    // configura a largura do canvas dinamicamente
    canvas.width = maxStrWidth[0] + 9;
    canvas.height = x * message.length;

    // seta a cor do background do canvas
    context.fillStyle = "#ffffe6";
    context.fillRect(0, 0, canvas.width, canvas.height);

    // escreve o texto
    context.font = font;
    context.fillStyle = "#000";
    message.forEach(e => {
        context.fillText(e, 3, y);
        y += x;
    });

    // gera a string base64
    let base64 = canvas.toDataURL("image/jpeg", 1);

    // chamada do plugin social share
    window.plugins.socialsharing.share(
        null, 
        'Comprovante de Aposta', 
        base64, 
        null
    );
}

The error is thrown on the var maxStrWidth line. 错误在var maxStrWidth行上引发。 Do you see anything wrong with it? 你觉得有什么问题吗?

Not all devices support the ES6 arrow functions, this is especially true for older android versions that use an older chrome webview version. 并非所有设备都支持ES6箭头功能,对于使用较旧chrome webview版本的较旧android版本尤其如此。

If you intend on supporting older devices it may be better to stick with standard function declarations. 如果您打算支持较旧的设备,则最好坚持使用标准函数声明。

Further reading here: 在这里进一步阅读:

https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/ https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM