Home >Web Front-end >JS Tutorial >How to use JSON as function parameter
This time I will show you how to use JSON as a function parameter, what are the precautions when using JSON as a function parameter, the following is a practical case, let’s take a look one time.
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 / to a function at any time Delete parameters or add new parameters at any parameter position, unlike traditional parameters that must be written in the specified order, 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 following code demonstration:
<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>
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 they only need to pass in a few or one of the parameters.
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>
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 dig into and learn json.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Summary of methods for converting JSON strings and JSON objects into each other
The above is the detailed content of How to use JSON as function parameter. For more information, please follow other related articles on the PHP Chinese website!