函数参数是否支持 JavaScript 中的默认值? [英] Do function arguments support a default value in JavaScript?

查看:29
本文介绍了函数参数是否支持 JavaScript 中的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看与将 PHP 函数移植到 JavaScript 相关的问题时.我看到了我认为不正确的 JavaScript:

While looking at a question related to porting a PHP function to JavaScript. I saw what I assumed was incorrect JavaScript:

function my_isnum(str, negative=false, decimal=false)

然后我在 JSFiddle 中尝试了这个:

Then I tried this in JSFiddle:

function my_isnum(str, negative=false, decimal=-2)
{
    console.log(arguments);
    console.log(str);
    console.log(negative);
    console.log(decimal);
}
my_isnum("5", "Hi");

令我惊讶的是,这就是我在 Firebug 控制台中看到的:

And to my utter amazement this is what I see in the Firebug console:

["5", "Hi"]
5
Hi
-2

现在在 Chrome 中这是我看到的:

Now in Chrome this is what I see:

Uncaught SyntaxError: Unexpected token = 

我不明白的是这是 Firefox 支持的一些早期标准的示例(function 似乎没有提到这个)?

What I don't understand is this an example of some early standard being supported by Firefox (the MDN on function doesn't seem to mention this)?

推荐答案

这似乎在 ECMAScript 6 规范中,目前只有 Firefox 支持

This seems to be in the ECMAScript 6 specification and is at the moment only supported by Firefox

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/default_parameters

这篇关于函数参数是否支持 JavaScript 中的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆