var
rootMenu
=
new
Ext.tree.TreeNode({
text: " 示例中心 " ,
expanded: true
});
Ext.MessageBox.show({
title: " 消息提示区内容 " ,
msg:hArea.dom.innerHTML,
width: 200 ,
buttons:Ext.MessageBox.OK,
animEl:hArea
});
text: " 示例中心 " ,
expanded: true
});
Ext.MessageBox.show({
title: " 消息提示区内容 " ,
msg:hArea.dom.innerHTML,
width: 200 ,
buttons:Ext.MessageBox.OK,
animEl:hArea
});
使用过Ext的朋友可能会发现,在其所有的API中大量用到了JSON对象,Ext使用类如配置的方式来生成各个页面对象等,其特点就是函数的参数大都是json对象,其主要特点就是函数的参数大都是json对象。
如
等等此类的用法在Ext中到处可见,那么那样写有什么优点呢?为什么不直接写函数参数而要通过JSON对象作为参数传入?
今日查看了Ext的帮助说明才真正认识到使用json对象作为函数参数的优点。
总结两点:
1、程序灵活性高,容易扩展和使用。
我们可以随时为一个函数添加/删除参数或在任意参数位置添加新参数,而不会像传统参数一样必须按照规定的顺序书写,而且每次添加或删除函数某个参数都要函数内容进行修改。而使用JSON作为参数可以不用考虑参数顺序。
看下面代码演示:
<
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 >
普通函数缺点很明显:必须记忆参数意义和顺序。而使用JSON的方法则不需要。
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方法优点很明显:每次只需传入需要的参数即可,而不用考虑其他参数。
JSON真是一个好用的东西,不用在ajax方面还是在js的其他方面都体现了其方便灵活的特性,的确应该好好去挖掘和学习json。