Home > Web Front-end > JS Tutorial > Advantages and Disadvantages of Using JSON as Parameters of Functions

Advantages and Disadvantages of Using JSON as Parameters of Functions

高洛峰
Release: 2016-12-08 14:54:03
Original
1276 people have browsed it

I have never understood json very well. I recently read some introductions and found out that this thing is so easy to use. Let me introduce it to you below

1. We can add/delete parameters to a function at any time or add new ones at any parameter position. Parameters do not have to be written in a prescribed order like traditional parameters, and each time a parameter of the function is added or deleted, the function content must be modified. When using JSON as a parameter, you don't need to consider the order of parameters.
Look at the code demonstration below:

<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>
Copy after login

The shortcomings of ordinary functions are obvious: the meaning and order of parameters must be remembered. This is not required when using JSON.

2. It is very convenient for users to call functions, especially when only a few or one of the parameters need to be passed in.
Look at the code demonstration below:

<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>
Copy after login

The advantages of the JSON method are obvious: you only need to pass in the required parameters each time, without considering other parameters.
JSON is really a useful thing. It does not need to be used in ajax or other aspects of js. It reflects its convenient and flexible characteristics. You should really explore and learn json.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template