Comparison of js URL parameter concatenation Methods

Source: Internet
Author: User

The most common method is:
Copy codeThe Code is as follows: url? Arg1 = value1 & arg2 = value2 & arg3 = value3...
This method is the most common, common, and easy to understand. However, in a project, if the following parameters are variable and the field has different values or different semantics, in this way, maintainability and readability are not high, and the code can be easily duplicated or redundant.
For example, the following request string for CGI:
Copy codeThe Code is as follows:
Var url = "http://www.tenpay.com/app/v1.0/juhui.cgi? ";
Var queryString = "method = 2 & page = index ";
If (content a) {// area A of the homepage to be accessed, you need to add the parameter subpage
QueryString + = "& subpage = ";
} Else if (content B) {// if region B is accessed, the subpage parameter is changed to B
QueryString + = "& subpage = B ";
}
If (spec_method ){
// If you need to follow the specified filtering method during viewing, you also need to add the spec_method Parameter
QueryString + = "& spec_method = 1"
}

This is the most common spelling logic. Such code is of no problem, but it is very troublesome to write comments, and it is not readable, and the field description is not clear, if you need to replace a field or change the logic based on the original one, it is troublesome.
I have read some colleagues' solutions to this problem. The first is to save the parameters in the form of objects, and then write a method to combine the parameters during the request:
Copy codeThe Code is as follows:
Var queryConfig = {
"Page": "index ",
"Method": 2, // 1: View by method A 2: View by method B
"Subpage":-1, //-1: This condition is not passed. a: View contentA B: View contentB
"Spec_method":-1 //-1: This condition is not passed. 1: query by sales volume. 2: query by time.
};
Var setQueryConfig = function (){
Var _ str = "";
For (var o in queryConfig ){
If (queryConfig [o]! =-1 ){
_ Str + = o + "=" + queryConfig [o] + "&";
}
}
Var _ str = _ str. substring (0, str. length-1 );
Return _ str;
}

This method is quite good. The advantage is that all parameters are clearly listed in the object. Annotations can also be more detailed for fields, improving readability and maintainability; however, the disadvantage is that there are a lot of code and a special method is needed to combine parameters.
Another method is to use the array method:
Copy codeThe Code is as follows:
Var queryString = [
"Method = 2", // comment on the method Field
"Page = index"
];
If (content a) {// area A of the homepage to be accessed, you need to add the parameter subpage
QueryString. concat ([
"Subpage = a", // subpage comment
]);
} Else if (content B) {// if region B is accessed, the subpage parameter is changed to B
QueryString. concat ([
"Subpage = B", // subpage comment
]);
}
If (spec_method ){
// If you want to follow the specified filtering method, you also need to add the queryString. concat ([
"Spec_method = 2", // spec_method Annotation
]);
}
QueryString = queryString. join ("&");

This method may be less readable than the object method, but it also has a high maintainability and less code. I prefer this method.
Well, if it weren't for yesterday's code review, it would take me a long time to find out code optimization in such a small place. It seems that the Code review is an accelerator for improving its own capabilities, haha.
I have found these two methods for the time being. If I find any good methods later, I will add them ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.