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 a JS 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 other Ajax 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 call nativeAjax 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 of ajax 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 <script> internal Just use wrap.service(`ajax`, foo), and then the foo function has the service attribute of ajax inside. We only need to bind the ajax attribute to this pointer. The get of ajax includes URL, TYPE, and SUCCESS. ERROR has four attributes, and all four attributes support uppercase and lowercase formats. Then this.success and this.error are two methods, which are to call the successful data request and to call the error status capture, and both this.success and this.error have a parameter. </script>
wrap.service('ajax',function ajax() { // 支持大小写 this.URL = "query.do" this.TYPE = "GET" this.SUCCESS = function(data) { var val = data; console.log(val) }; this.ERROR = function(err) { console.log(err) }; });
Version 3rewrap-ajax maintains the external structure and this writing method of version 2. There are two more functions on the this pointer, namely props and methods methods. The props method returns internally. The structure is a data format of key-value pairs, with multiple state... states, such as:
1 return { 2 State_01: [{ class : ‘.main’, static: 'color', tip: 'message', content: 'container'}], 3 State_02: [{ class : ‘.main’, static: 'color'}], 4 State_03: [{ class : ‘.main’}] 5 }
The value corresponding to each state state must be saved using an array [], The interior of the array must be an object {}. The attributes of the object are required in a regular format. The value corresponding to the attribute of the object must be an Element element (or node node, class, id, tag label, etc.) that can be accessed by a dom node.
However, the internal structure of the methods method is in the format of a regular object call function, in which the object key returned by return is the API method. Note that it does not support custom writing. The returned object is The value is a function writing method, then the function method receives a parameter (this parameter is the state attribute inside the props pipeline), and the method uses this pointer writing method internally, as follows: this.el(param).add()
Complete structure:
return { addClass: function (scope){ this.el([scope.class,scope.static]).add() }, removeClass: function (scope){ this.el([scope.class,scope.tip]).remove() }, pushHtml: function (scope){ this.el([scope.static,scope.class]).push() }, hasClass: function (scope){ this.el(scope.define.content).has() } }
Wherein the parameter inside this.el(param) method is the status attribute value flowing out through the props pipeline as a parameter, and the result received by the parameter is a string, multiple characters The string results can be stored in the form of array [], then these strings have this API method. The add method after this.el() is the add method called by the current string. However, the add binding method is an API method. For example, addClass is the API method inside rewrap-ajax, so you call your custom add externally. method to implement the addClass method. Note that the customized add method within this.methods does not require parameters, but whether external pipeline calls to add require parameters depends on the addClass method (addClass is the API method inside rewrap-ajax). So the API methods will be exposed later.
The method of external this call is to obtain the props attribute and methods method through the scope $scope. The external call is as follows:
this.ERROR = function( $scope, err ) { $scope.$props.$el($scope.$props.$scope.define.static).add('error_message') $scope.$props.$el($scope.$props.$scope.define.static).push('调用出错 error') $scope.$props.$el($scope.$props.$scope.define.tip).remove('show') console.log(err) }
The above is the detailed content of Introduction to rewrap-ajax.js plug-in. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version