Is there a way to provide named parameters in a function call in JavaScript?

前端 未结 10 1718
抹茶落季
抹茶落季 2020-11-22 07:28

I find the named parameters feature in C# quite useful in some cases.

calculateBMI(70, height: 175);

What can I use if I want this in JavaS

10条回答
  •  无人及你
    2020-11-22 07:42

    Trying Node-6.4.0 ( process.versions.v8 = '5.0.71.60') and Node Chakracore-v7.0.0-pre8 and then Chrome-52 (V8=5.2.361.49), I've noticed that named parameters are almost implemented, but that order has still precedence. I can't find what the ECMA standard says.

    >function f(a=1, b=2){ console.log(`a=${a} + b=${b} = ${a+b}`) }
    
    > f()
    a=1 + b=2 = 3
    > f(a=5)
    a=5 + b=2 = 7
    > f(a=7, b=10)
    a=7 + b=10 = 17
    

    But order is required!! Is it the standard behaviour?

    > f(b=10)
    a=10 + b=2 = 12
    

提交回复
热议问题