Example _ javascript tips for retrieving ajax response objects using the hasOwnProperty method in Js

Source: Internet
Author: User
Tags hasownproperty
This article describes how to use the hasOwnProperty method in Js to retrieve ajax response objects. The technique described in this article is the use of the hasOwnProperty method in ajax requests, if you need a friend, you can refer to those who often use Baidu search. The drop-down index of the input box will not be ignored. It is so convenient. However, the unique condition makes this asynchronous technology somewhat challenging, highly Concurrent Server requests urge their front-end attacker to minimize the number of ajax requests. It sounds unrelated to this article, but it is not like this. First, let's make a free advertisement for Baidu. Enter the word "front-end" on the Baidu homepage and use chromebug to easily view the sent response. The result is as follows:

The Code is as follows:


Window. bdsug. sug ({q: 'frontend ';, p: false, s: ['frontend developer', 'frontend engineer', 'frontend bus', 'frontend engineer ', 'front-end framework', 'front-End bus frequency ', 'front-end interview test', 'front-end analytics', 'front-end development tools', 'front-end Inspector ']});

Baidu tries to render the drop-down data by returning a sug method with the obj parameter. If you do not click the new page, enter "front-end" again ", similar requests do not occur, which means they may have created a cache object. It is used to temporarily store the requested object. When the same word is entered, the key of the cached object will be retrieved first. After successful matching, the value of the object will be directly read and no request will be sent to the server. This will effectively save costs.

Let's talk about the hasOwnProperty method.

I believe that jser is no stranger to hasOwnProperty. I am here to sell water.

It is the exclusive object. It is used to determine whether an attribute exists in the key of an object. The return value is a boolean value. This is an example:

The Code is as follows:


Var test0 = Array. prototype. hasOwnProperty ('split '); // false, because the Array does not have the split method
Var test1 = String. prototype. hasOwnProperty ('split '); // true, because split is a built-in method of the String object

When you know this, it seems that it is not enough to see the hasOwnProperty force, so the following is a simple example of Baidu pull-down:

The Code is as follows:


Var data ={}, saveObj = function (val ){
If (data. hasOwnProperty (val) {// if the submitted value exists in the data object, no request is sent.
Var len = data [val]. length;
For (var I = 0; I <len; I ++ ){
Console. log (data [val] [I]);
}
} Else {
Var url = 'HTTP: // suggestion.baidu.com/su? Wd = '+ val;
$. Ajax ({// For example, jquery's ajax is used as an example.
Url: url + '& p = 3 & cb = window. bdsug. sug & sid = 1421_1513_1541_1542_1582 & t = 1353756355137', // The last parameter t is a timestamp.
DataType: 'jsonp ',
Type: 'get ',
Success: function (res ){
Var msg = res. data, len = msg. length;
Msg = null & (msg = []);
If (len> 0 ){
Data [val] = msg; // cache search results
For (var I = 0; I <len; I ++ ){
Console. log (msg [I]); // print the request result
}
}
}, Error: function (){
Alert ('error ');
}
});
}
};

Some colleagues questioned that the memory occupied by the cached object data will increase as the storage key value increases. So I would like to say that this is inevitable. To save server-side requests, we must sacrifice others. In fact, the space occupied by cache objects is usually negligible, because it is not "resident memory", it will be destroyed once the page is refreshed. However, we can provide another solution to specify a peak value for this object. For example, a maximum of 100 key-value pairs can be stored. When the number of key-value pairs exceeds 100, you can use the delete operator to delete the top 10 keys or simply not store them. This avoids this problem.

The hasOwnProperty method is particularly common in object detection. Of course, if you are interested, you can also use the propertyIsEnumerable method, which is an enhanced version of hasOwnProperty, this article will not detail how to check its own attributes and Their enumerable properties.

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.