<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>es6 函数参数默认值</title>
</head>
<body>
<script>
// es5
function makeAjaxRequest (url,method) {
if(!method){
method = "GET";
}
return method;
}
//es6
function makeAjaxRequest (url,method = "GET") {
return method;
}
console.log(makeAjaxRequest("baidu.com"));
console.log(makeAjaxRequest("baidu.com","POST"));
function User (id) {
this.id = id;
}
function randomId () {
return Math.random()*999999;
}
const user = new User(1);
function createAdminUser (user = new User(randomId ())) { // 不传参数时,参数为默认参数
user.admin = true;
return user;
}
// console.log(new User(randomId()));
console.log(createAdminUser());
console.log(createAdminUser(user)); // 传递参数
</script>
</body>
</html>
es6 函数参数默认值
最新推荐文章于 2024-05-28 13:52:26 发布