Uncaught SyntaxError: Unexpected token = in Google Chrome

后端 未结 4 1003
傲寒
傲寒 2020-12-18 20:16

I have a javascript function which accept an optional parameter. This works fine in Firefox, but in Google Chrome it shows:-

Uncaug         


        
相关标签:
4条回答
  • 2020-12-18 20:47

    You are using a default parameter feature which is supported only by Firefox now.

    function errorNotification(text) {
        text = text || "Something went wrong!";
        $.pnotify({
            title: 'Error',
            text: text,
            type: 'error'
        });
    }
    
    0 讨论(0)
  • 2020-12-18 20:55

    Note: The other answers won't work if you have a boolean value that you want to default to true. That's because anything||true will always return true. Instead you should do something like:

    function foo(bar){
      if(bar === undefined)
        bar = true;
      //rest of your function
    }
    
    0 讨论(0)
  • 2020-12-18 21:01

    Javascript does not allow you to pass default arguments like that. You need to assign the default internally to the function.

    function errorNotification(text) {
      text || (text = "Something went wrong!");
      $.pnotify({
          title: 'Error',
          text: text,
          type: 'error'
      });
    }
    
    0 讨论(0)
  • 2020-12-18 21:05

    function [a-zA-Z0-9_]*?\s{0,}\([^=$)]*?(=)[^)$]*?\) it can be helpful to find js functions with default parameter and then correct them.

    0 讨论(0)
提交回复
热议问题