0

javascript 函数怎样设置参数默认值?

gail created at6 years ago view count: 2389
report
回复
0

ES6/ES2015, 可以想其他语言一样, 直接在函数定义里面写。

function read_file(file = '/tmp/log') {
}

ES2015前的需要判断是否定义,然后在去设置

function read_file(file) {
    file = typeof file !== 'undefined' ? file : '/tmp/log';
}
6 years ago 回复