I haven’t written technical articles for a long time. I have been writing diaries during this process. The recording of daily life has replaced the writing of technical articles. It can be seen that in the past, my heart was full of passion or fire, but now it is. ..
Recently I wrote aJS plug-in. In the words of the circle, it is called a wheel. Whether it is good or not is not up to you. The key is what everyone uses. Good and bad.
Of course I am also using it myself. Due to my personal preferences and preferences, I cannot borrow the writing methods of otherAjax frameworks. The current version is the result of upward integration from version one, so you If you want to fully understand the internal structure, you can start with version 1.
Now let’s talk about the overall design structure. Version 1 is a method of collecting and organizing friends. The basic writing method has been formed. Just callnativeAjax and put it. There are three parameters inside the method. The first One is an ajax attribute, the second is a successful function, and the third is an error function. The details are as follows:
nativeAjax(postOption,function(data){ // 3.1、请求成功回调 console.log(data); },function(error){ // 3.2、请求失败回调,返回HTTP状态码 console.log(error); });
Based on the above design pattern, then encapsulate it twice, First of all, the service properties ofajax are fixed, so we can bind all ajax properties to the object. If they are directly bound to the specified object, in fact, we only need to give and take parameters, which saves a lot. Things, this process is much simpler, so the flexibility will not be very high. If we want to encapsulate it internally, we need high flexibility and reusable attributes, so I put the attributes in the function function, so I use this pointer to bind the ajax attribute internally to call it, and at the same time, I can do it internally. The re-encapsulation of the plug-in is the design pattern of the internal structure of this version as I understand it.
Version 2The external structure called by rewrap-ajax is similar to the JQ.fn attribute of jq. It is used by the object attribute method of the chain structure, so we use the