我在这里看到了一些关于此事的问题:
This is good
function(a, b) { a = (typeof a === 'undefined')? 'default_val' : a; b = (typeof b === 'undefined')? 'default_val' : b; }
This is somewhat troublesome (works for anything but boolean values)
function(a, b) { a = a || 'default_val'; b = b || 'default_val'; }
If you pass false it break the logic. So this should be marked as a bad habit
There were a few question, but I have never seen the answer to this (nobody answered directly to the question): is this (last method) good (does anyone sees any issues with it) ?
function(a = 'default_val', b = 'default_val') { // your code }
谢谢。
undefined
值传递到函数中。您必须检查arguments.length
来完成这项工作。 - Andrew D.