Javascript is a bit confusing

Source: Internet
Author: User

I suddenly felt that the current JavaScript library design is more or less inconvenient.

Especially when using an object for config.

 

For exampleCodeWe should be quite familiar:

 

 1$. Ajax ({

2 URL: '/books/123 ',
3 Type: 'get ',
4 Cache: False
5 });

 

OK. How do we know the name of some complicated configurations? For example, the author supports Content-Type, data-type, mime-type, or what other names does he use?

We don't know. We can only query documents. Where to check the document is often a headache for us. Some open-source things have no documentation at all. You want to know how to use it? Check the code.

 

In fact, the designer only needs to make a slight change.

For example:

 

 1(Function(){

2 VaR Cloneobject = Function (OBJ ){
3 VaR Result = {};
4 For (Name In OBJ ){
5 Result [name] = OBJ [name];
6 }
7 Return Result;
8 };
9 VaR Defaultconfig = {
10 Cache: False ,
11 Contenttype: 'text/html'
12 };
13 Window. $ = {};
14 $. Ajax = Function (Config ){
15 VaR Ajaxconfig = config (cloneobject (defaultconfig ));
16 };
17
18 $. Ajax ( Function (Config ){
19 Config. cache = False ;
20 });
21 })();

 

Then, if you do not know how to write in config when you can call it like this?

That's good. You can set the breakpoint to the place of the call and check what the config looks like.

 

The default config does not need to exist. Is there any performance consumption that causes performance problems?

I know some perfectionists will definitely ask this question.

The answer is as follows:

 

1. Even if the solution is in use, there are only a few items in config. The Config you provide may be thrown in. It also performs merge with a default one and then uses it again. If so, the performance will be the same. It even saves a merge.

2 does your Config have thousands of configuration items? If yes, it will indeed produce performance problems, but believe me, you will first face design problems, but performance problems are the second.

3. This is a controllable performance problem. Even if it is slow, optimization is the simplest. For example, you can use a shared config to performArticle. Set only intercepts the next record, while the value of the shared config object remains unchanged.

Besides, this is just a piece of thinking.

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.