// Common method
function commonArg(name,age,desc){
var userinfo="name: "+name+","+"age: "+age+"\ndescription: "+desc;
alert(userinfo);
}
commonArg("yemoo".22."a boy!")
// Defects: 1. If there are too many parameters, the length in parentheses is too long
// 2. The parameters can be transmitted only in the order of the parameters. Otherwise, the returned information is incorrect
// 3. If you do not want to pass the parameter, you need to set null
//JSON parameter transmission mode
function JsonArg(json){
this.name = json.name;
this.age = json.age;
this.desc = json.desc;
}
JsonArg.prototype.user = function(){
console.log('name:'+this.name+'age:'+ this.age + 'features:+ (this.desc||empty));
}
var jsonArg1 = new JsonArg({name:'chen'.age:20.desc:'It's a man'});
var jsonArg2 = new JsonArg({name:'chen'.age:20});
jsonArg1.user();
jsonArg2.user();
Copy the code
- You can reduce the number of arguments passed in. All you have to do is pass one in
- Since JSON has a key value, there is no need for pastoralist order when passing arguments
- You only need to pass in the required parameters each time
- JSON, try to use a single layer structure