varrootMenu=newExt.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作为参数可以不用考虑参数顺序。
看下面代码演示:
<scripttype="text/javascript">
<!--
//普通方法
functioncommonArg(name,age,desc){
varuserinfo="name:"+name+","+"age:"+age+"\ndescription:"+desc;
alert(userinfo);
}
commonArg("yemoo",22,"aboy!")//每次调用都必须按照规定的参数顺序书写,如果写成commonArg(22,"yemoo","desc")则返回信息就不对,每次都必须记忆每个参数意义及顺序
//JSON参数法
functionjsonArg(info){
varuserinfo="name:"+info.name+"\tage:"+info.age+"\ndescription:"+info.desc;
alert(userinfo);
}
jsonArg({name:"blue",age:22,desc:"agril?"});
jsonArg({desc:"notapeople!",name:"sss",age:0});
//参数位置可以随意写
//-->
</script>
<!--
//普通方法
functioncommonArg(name,age,desc){
varuserinfo="name:"+name+","+"age:"+age+"\ndescription:"+desc;
alert(userinfo);
}
commonArg("yemoo",22,"aboy!")//每次调用都必须按照规定的参数顺序书写,如果写成commonArg(22,"yemoo","desc")则返回信息就不对,每次都必须记忆每个参数意义及顺序
//JSON参数法
functionjsonArg(info){
varuserinfo="name:"+info.name+"\tage:"+info.age+"\ndescription:"+info.desc;
alert(userinfo);
}
jsonArg({name:"blue",age:22,desc:"agril?"});
jsonArg({desc:"notapeople!",name:"sss",age:0});
//参数位置可以随意写
//-->
</script>
普通函数缺点很明显:必须记忆参数意义和顺序。而使用JSON的方法则不需要。
2、使用者对函数的调用非常方便,特别是在只需要传入其中几个或一个参数时。
看下面的代码演示:
<scripttype="text/javascript">
<!--
//普通方法
functioncommonArg(name,age,desc){
varuserinfo="name:"+(name||"empty")+"\tage:"+(age||0)+"\ndescription:"+desc||"empty";
alert(userinfo);
}
//当只需要设定后面的参数时前面每个参数都需要设置为null
commonArg("tempUser");
commonArg(null,null,"aboy!");
commonArg(null,20);
//JSON参数法
functionjsonArg(info){
varuserinfo="name:"+(info.name||"empty")+"\tage:"+(info.age||0)+"\ndescription:"+(info.desc||"empty");
alert(userinfo);
}
//每次只需要设置需要设定参数项
jsonArg({name:"tempUser"});
jsonArg({desc:"aboy!"});
jsonArg({age:20});
//-->
</script>
<!--
//普通方法
functioncommonArg(name,age,desc){
varuserinfo="name:"+(name||"empty")+"\tage:"+(age||0)+"\ndescription:"+desc||"empty";
alert(userinfo);
}
//当只需要设定后面的参数时前面每个参数都需要设置为null
commonArg("tempUser");
commonArg(null,null,"aboy!");
commonArg(null,20);
//JSON参数法
functionjsonArg(info){
varuserinfo="name:"+(info.name||"empty")+"\tage:"+(info.age||0)+"\ndescription:"+(info.desc||"empty");
alert(userinfo);
}
//每次只需要设置需要设定参数项
jsonArg({name:"tempUser"});
jsonArg({desc:"aboy!"});
jsonArg({age:20});
//-->
</script>
JSON方法优点很明显:每次只需传入需要的参数即可,而不用考虑其他参数。
JSON真是一个好用的东西,不用在ajax方面还是在js的其他方面都体现了其方便灵活的特性,的确应该好好去挖掘和学习json。
本文探讨了在Ext框架中使用JSON对象作为函数参数的优势,包括提高程序灵活性和方便函数调用两个方面。通过对比普通参数传递与JSON参数传递的优缺点,展示了JSON在简化参数管理、增强代码可读性和维护性方面的显著效果。
274

被折叠的 条评论
为什么被折叠?



