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

查看:143
本文介绍了不要函数参数支持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 尝试这样:

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 = 

我不明白这是火狐(由MDN上<一所支持的一些早期的标准为例href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/function\"><$c$c>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

<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/default_parameters\">https://developer.mozilla.org/en-US/docs/JavaScript/Reference/default_parameters

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

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