看下面代码演示:
<
script type
=
"
text/javascript
"
>
<!--
// 普通方法
function commonArg(name,age,desc){
var userinfo = " name: " + name + " , " + " age: " + age + " \ndescription: " + desc;
alert(userinfo);
}
commonArg( " yemoo " , 22 , " a boy! " ) // 每次调用都必须按照规定的参数顺序书写,如果写成commonArg(22,"yemoo","desc")则返回信息就不对,每次都必须记忆每个参数意义及顺序
// JSON参数法
function jsonArg(info){
var userinfo = " name: " + info.name + " \tage: " + info.age + " \ndescription: " + info.desc;
alert(userinfo);
}
jsonArg({name: " blue " ,age: 22 ,desc: " a gril? " });
jsonArg({desc: " not a people! " ,name: " sss " ,age: 0 });
// 参数位置可以随意写
// -->
< / script>
<!--
// 普通方法
function commonArg(name,age,desc){
var userinfo = " name: " + name + " , " + " age: " + age + " \ndescription: " + desc;
alert(userinfo);
}
commonArg( " yemoo " , 22 , " a boy! " ) // 每次调用都必须按照规定的参数顺序书写,如果写成commonArg(22,"yemoo","desc")则返回信息就不对,每次都必须记忆每个参数意义及顺序
// JSON参数法
function jsonArg(info){
var userinfo = " name: " + info.name + " \tage: " + info.age + " \ndescription: " + info.desc;
alert(userinfo);
}
jsonArg({name: " blue " ,age: 22 ,desc: " a gril? " });
jsonArg({desc: " not a people! " ,name: " sss " ,age: 0 });
// 参数位置可以随意写
// -->
< / script>
2、使用者对函数的调用非常方便,特别是在只需要传入其中几个或一个参数时。
看下面的代码演示:
<
script type
=
"
text/javascript
"
>
<!--
// 普通方法
function commonArg(name,age,desc){
var userinfo = " name: " + (name || " empty " ) + " \tage: " + (age || 0 ) + " \ndescription: " + desc || " empty " ;
alert(userinfo);
}
// 当只需要设定后面的参数时前面每个参数都需要设置为null
commonArg( " tempUser " );
commonArg( null , null , " a boy! " );
commonArg( null , 20 );
// JSON参数法
function jsonArg(info){
var userinfo = " name: " + (info.name || " empty " ) + " \tage: " + (info.age || 0 ) + " \ndescription: " + (info.desc || " empty " );
alert(userinfo);
}
// 每次只需要设置需要设定参数项
jsonArg({name: " tempUser " });
jsonArg({desc: " a boy! " });
jsonArg({age: 20 });
// -->
< / script>
<!--
// 普通方法
function commonArg(name,age,desc){
var userinfo = " name: " + (name || " empty " ) + " \tage: " + (age || 0 ) + " \ndescription: " + desc || " empty " ;
alert(userinfo);
}
// 当只需要设定后面的参数时前面每个参数都需要设置为null
commonArg( " tempUser " );
commonArg( null , null , " a boy! " );
commonArg( null , 20 );
// JSON参数法
function jsonArg(info){
var userinfo = " name: " + (info.name || " empty " ) + " \tage: " + (info.age || 0 ) + " \ndescription: " + (info.desc || " empty " );
alert(userinfo);
}
// 每次只需要设置需要设定参数项
jsonArg({name: " tempUser " });
jsonArg({desc: " a boy! " });
jsonArg({age: 20 });
// -->
< / script>
JSON真是一个好用的东西,不用在ajax方面还是在js的其他方面都体现了其方便灵活的特性,的确应该好好去挖掘和学习json。