一、对位传参
function fn1(name,sex,age){ console.log("用户的姓名为 ",name,",性别为 ",sex,",年龄为 ",age); } fn1("tjh","男",18); --》用户的姓名为 tjh ,性别为 男 ,年龄为 18
讯享网
二、对像传参
讯享网function fn1(porent){ console.log("用户的姓名为 ",porent.name,",性别为 ",porent.sex,",年龄为 ",porent.age); } fn1({name: "tjh",sex: "男",age: 18}); --》用户的姓名为 tjh ,性别为 男 ,年龄为 18
三、参数默认值(三种方式)
//ES6的写法 function fn1(porent = {name: "lj"}){ console.log("用户的姓名为 ",porent.name,",性别为 ",porent.sex,",年龄为 ",porent.age); } fn1({name: "tjh"}); --》用户的姓名为 tjh ,性别为 undefined ,年龄为 undefined
讯享网//ES5的方法 function fn1(porent){ porent.name = (porent.name == undefined ? "tjh":porent.name), porent.sex = (porent.sex == undefined ? "女":porent.sex) console.log("用户的姓名为 ",porent.name,",性别为 ",porent.sex,",年龄为 ",porent.age); } fn1({name: "tjh",age: 20}); --》用户的姓名为 tjh ,性别为 女 ,年龄为 20
//ES5的方法 function fn1(porent){ porent.name = (porent.name || "wzh"), porent.sex = (porent.sex || "中") console.log("用户的姓名为 ",porent.name,",性别为 ",porent.sex,",年龄为 ",porent.age); } fn1({name: "t",age: 20}); --》用户的姓名为 t ,性别为 中 ,年龄为 20 //此方法有缺陷,当传入的值为0的时候,会传入默认值
四、Arguments(将参数以伪数组的形式传入进行操作,有下标)
讯享网function f1(n1,n2,n3,n4,n5){ let arr = Array.prototype.slice.call(arguments); //将伪数组转化为真数组,用call方法修改this指向,调用数组的方法 return arr.reduce(function(ele1,ele2){ return ele1 + ele2; }) } f1(1,2,3,4,5); --》15
function f1(n1,n2,n3,n4,n5){ let arr = Array.from(arguments); return arr.reduce(function(ele1,ele2){ return ele1 + ele2; }) } f1(1,2,3,4,5); --》15

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/100823.html