Js Object-Oriented Programming: data caching and js Object-Oriented Programming caching
Js can also cache data to speed up processing. It is worthwhile to use space in exchange for time when necessary. For example, if it takes a long time to complete the computation, You Can cache the computation results to the client. Later, you can directly use the cached computation results without repeated computation.
1 cache of calculation results of simple functions 2 cache of calculation results of recursive functions 3 cache of data read by Ajax
1 cache the computing results of simple functions
For example:
// The shared function encapsulates internal calls and caches the calculation result function memorize (f) {var cache ={}; return function () {var key = arguments. length + Array. prototype. join. call (arguments, ","); // the length of the parameter and the value of the parameter as the property value if (key in cache) // if yes, the cached data is directly read {return cache [key];} else {return cache [key] = f. apply (this, arguments); // if no value exists, the calculation result is calculated and cached }}}
Usage
// Simulate the function TestFunction (x) {return x * x;} // test, call function test () {var t = memorize (TestFunction ); var k = t (6); // The first call requires Calculation of var k2 = t (6); // The second call directly reads the cache instead of alert (k2 );}
2
Cache the computing result of recursive functions
Usage
Function test2 () {var t = memorize (function (x) {// recursive function compute X factorial if (x <= 0) {return 1 ;} else {return x * t (x-1)}); var k = t (6); // The factorial var k2 = t (7) cached from 6 to 1 ); // only one call is required. alert (k2) is cached when the 6-step multiplication is calculated );}
Cache for reading data from 3Ajax
You can also use a similar method for Ajax caching. In fact, you can still learn from https://github.com/devbridge/jquery-autocomplete.
The principle is actually the same as above.
The core section is as follows:
Function Autocomplete () {this. cachedResponse ={}; // container for storing cached data this. badQueries = []; // query that does not meet the condition} Autocomplete. prototype. export ache = function () // clear cache {this. cachedResponse ={}; this. badQueries = [];}, Autocomplete. prototype. getSuggestions = function (q) {// read var response, that = this, options = that. options, serviceUrl = options. serviceUrl, data, cacheKey; options. params [options. paramName] = Q; data = options. ignoreParams? Null: options. params; cacheKey = serviceUrl + '? '+ $. Param (data | {}); // The cached key is composed of URL and passed parameters. response = that. cachedResponse [cacheKey]; // obtain the cached data if (response & $. isArray (response. suggestions) {// if there is a cache that. suggestions = response. suggestions; that. suggest ();} else if (! That. isBadQuery (q) {// No query is performed, and it is not a query that does not meet the conditions after the query. currentRequest = $. ajax ({// No cache for reading data through Ajax url: serviceUrl, data: data, type: options. type, dataType: options. dataType }). done (function (data) {that. currentRequest = null; that. processResponse (data, q, cacheKey); // cache data options. onSearchComplete. call (that. element, q );}). fail (function (jqXHR, textStatus, errorThrown) {options. onSearchError. call (that. element, q, jqXHR, textStatus, errorThrown );});}}
We can see that the core is the same as the above, but the cache data key is changed to the Ajax URL and passed parameters.
How does js clear the js script cache on the page? js Code on the jsp page controls data changes, such as deleting a piece of data, that is, after deleting a node,
First, you can delete the page node. This will only affect the Display Effect of the client ), if you want to delete a server segment (from a database), you can use either of the following methods:
1 AJAX
2 Form submission
Both methods require the combination of backend code (Java). You have obfuscated the relationship between Java and Java,
In addition, this is not a cache, but the operation method is incorrect.
JS Object-Oriented Programming: How B inherits
B. prototype = newa;