jQuery Javascript 函数和默认参数,不适用于 IE 和 Chrome

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15178700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:33:30  来源:igfitidea点击:

Javascript Functions and default parameters, not working in IE and Chrome

javascriptjquery

提问by Talon

I created a function like this:

我创建了一个这样的函数:

function saveItem(andClose = false) {

}

It works fine in Firefox

它在 Firefox 中运行良好

In IE it gives this error on the console: Expected ')'

在 IE 中,它在控制台上给出了这个错误: Expected ')'

In Chrome it gives this error in the console: Uncaught SyntaxError: Unexpected token =

在 Chrome 中,它会在控制台中出现此错误: Uncaught SyntaxError: Unexpected token =

Both browsers mark the source of the error as the function creation line.

两个浏览器都将错误来源标记为函数创建行。

回答by juco

You can't do this, but you can instead do something like:

您不能这样做,但您可以执行以下操作:

function saveItem(andClose) {
   if(andClose === undefined) {
      andClose = false;
   }
}

This is often shortened to something like:

这通常缩写为:

function setName(name) {
  name = name || 'Bob';
}

Update

更新

The above is true for ECMAScript <= 5. ES6 has proposed Default parameters. So the above could instead read:

以上对于 ECMAScript <= 5 是正确的。 ES6 提出了Default parameters。所以上面可以改为:

function setName(name = 'Bob') {}

回答by the system

That's not a valid ECMAScript syntax, but it is a valid syntax for Mozilla's superset of features they add to their implementation of the language.

这不是有效的 ECMAScript 语法,但对于 Mozilla 的功能超集来说,这是一种有效的语法,它们添加到他们的语言实现中。

Default parameter assignment syntax is likelycoming in ECMAScript 6.

ECMAScript 6 中可能会出现默认参数分配语法。

回答by JRomero

Javascript does not allow a "default" specifier.

Javascript 不允许使用“默认”说明符。

A quick way of doing what you would want is changing:

做你想做的事情的一种快速方法是改变:

function saveItem(andClose = false) {

}

to the following:

到以下几点:

function saveItem(andClose) {
    // this line will check if the argument is undefined, null, or false
    // if so set it to false, otherwise set it to it's original value
    var andClose = andClose || false;

    // now you can safely use andClose
    if (andClose) {
        // do something
    }
}

回答by Max Heiber

The code you provided won't run in Chrome < version 49: https://kangax.github.io/compat-table/es6/#test-default_function_parameters

您提供的代码不会在 Chrome < 49 版中运行:https: //kangax.github.io/compat-table/es6/#test-default_function_parameters

You used valid ECMAScript 2015 syntax:

您使用了有效的 ECMAScript 2015 语法:

In my opinion, the best way to use ES2015 features is to bundle assets with Browserifyor WebPack, with a step for using Babelto trans-compile ES2015 to ES5. That way you don't have to worry about that ES2015 browser compatibility chart. It's a pain to get started the first time, but worth it.

在我看来,使用 ES2015 特性的最好方法是将资产与BrowserifyWebPack捆绑在一起,并有一个步骤是使用Babel将 ES2015 转编译为 ES5。这样你就不必担心 ES2015 浏览器兼容性图表。第一次开始很痛苦,但值得。

回答by Cédric Talpaert

In your case, you have an other alternative to be sure that your variable is a boolean:

在您的情况下,您有另一种选择来确保您的变量是布尔值:

function saveItem(andClose) {
  var andClose = true == andClose;
  // ...
}

Default value is undefined, and true == undefined=> false, so your default value will be false:)

默认值为undefined, 和true == undefined=> false,因此您的默认值为false:)