例:把人物信息的值传入函数进行处理。
简化为如下:css部分#wrap{ width: 100px; background: #0f0;}HTML部分<div id="wrap"></div>JS部分1.对象传参var oWrap = document.getElementById('wrap');var objs1 = { "name":"zack", "age":"23", "gender":"male"}// 或者参数名不带引号也行// * 注意标准的JSON格式是都带双引号的,用逗号隔开 *// var objs1 = { // name:"zack",// age:"23",// gender:"male"// }function fn1(obj){ var a = document.createElement("div"); var b = document.createElement("div"); var c = document.createElement("div"); oWrap.appendChild(a); oWrap.appendChild(b); oWrap.appendChild(c); a.innerHTML = obj.name; b.innerHTML = obj.age; c.innerHTML = obj.gender;}fn1(objs1);2.数组传参
//注意参数顺序var objs2 = ["muler","25","male"];function fn2(obj){ var a = document.createElement("div"); var b = document.createElement("div"); var c = document.createElement("div"); oWrap.appendChild(a); oWrap.appendChild(b); oWrap.appendChild(c); a.innerHTML = obj[0]; b.innerHTML = obj[1]; c.innerHTML = obj[2];}fn2(objs2);3.传不定量参数
//用函数自带的arguments代表了形参名//调用时注意实参顺序function fn3(){ var a = document.createElement("div"); var b = document.createElement("div"); var c = document.createElement("div"); oWrap.appendChild(a); oWrap.appendChild(b); oWrap.appendChild(c); a.innerHTML = arguments[0]; b.innerHTML = arguments[1]; c.innerHTML = arguments[2];}fn3("rosa","24","female");