为 JavaScript 函数设置一个默认参数值

I would like a JavaScript function to have optional arguments which I set a default on, which gets used if the value isn't defined. In Ruby you can do it like this:

def read_file(file, delete_after = false)
  # code
end

Does this work in JavaScript?

function read_file(file, delete_after = false) {
  // Code
}

转载于:https://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function

21个回答

Yes, This will work in Javascript. You can also do that:

function func(a=10,b=20)
{
    alert (a+' and '+b);
}

func(); // Result: 10 and 20

func(12); // Result: 12 and 20

func(22,25); // Result: 22 and 25
csdnceshi78
程序go It is not currently portable... IE... developer.mozilla.org/en/docs/Web/JavaScript/Reference/… (And it is a duplicate answer...)
3 年多之前 回复
共21条数据 首页 1 3
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问