【问题标题】:Resolve Uncaught SyntaxError: Unexpected token >解决 Uncaught SyntaxError: Unexpected token >
【发布时间】:2017-05-02 20:04:21
【问题描述】:

我有一个功能可以在我的 Cordova 应用程序上转换 base64 图像中的文本。它工作正常,但在少数设备中,脚本出现 Unexpected token 错误。

函数如下:

    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
    );
}

var maxStrWidth 行上引发错误。你觉得有什么问题吗?

【问题讨论】:

  • 这些设备似乎不支持粗箭头语法。尝试在该行上方的某处添加一个空的胖箭头函数,看看它是否在新行上出错。这会在哪些设备/浏览器上失败?
  • 该错误出现在许多不同的设备上,但它们大多是 4.X.X,很少是 5.5.1 Android。我正在使用 Chrome Inspect 调试应用程序。

标签: javascript android jquery cordova


【解决方案1】:

并非所有设备都支持 ES6 箭头功能,对于使用旧 chrome webview 版本的旧 android 版本尤其如此。

如果您打算支持旧设备,最好坚持使用标准函数声明。

在此处进一步阅读:

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

【讨论】:

  • 我更改了标准声明:message.forEach(e = function() { context.fillText(e, 3, y); y += x; });现在我得到:ReferenceError: invalid assignment left-hand side
  • 那是因为你的 forEach 函数的语法错误。 e = function() 应该更改为 function(e){} 所以在函数 e 中是当前消息值。
  • 像这样将函数作为参数传递? message.forEach(function(e) { context.fillText(e, 3, y); y += x; });
  • 哦,我错了,错误就在这里: var maxStrWidth = message.map(e = function() { return context.measureText(e).width; }).sort((a, b) = function() { return b - a; });我能做什么?
  • 再次,您应该将 e 作为参数传入。 e = 函数应该是函数(e){}
猜你喜欢
  • 1970-01-01
  • 2012-05-17
  • 2019-02-10
  • 2014-07-08
  • 2011-03-09
  • 2014-01-06
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多