Talk about the place in need of attention in JS

Source: Internet
Author: User
Tags add array object key return version tostring hasownproperty

In S. For, you can traverse the dominant property of an object or array, which means that our own defined attributes are traversed, and those that are already on the prototype are the ones that are available by default, such as: Object.prototype.toString, Object.prototype.hasOwnProperty is not to be traversed.

For the basic rules of in, but there are "pits" where we need to note:

1. The values for the in loop are not necessarily in order. The code is as follows:

var B = {3:1,42:2,11:3}
for (var key in B) {
Alert (B[key])
}

The order of the lower version browser window is: 1, 2, 3. The order of modern browser windows is 1, 3, 2.

2, add the extension method on the prototype, will be out for. The code is as follows:

Object.prototype.test = "I am Test"
var B = {"Name": "Txj"}
for (var key in B) {
Alert (key + ":" + B[key])
}

The method that we manually add to the prototype will be traversed when in. Generally we do not need to traverse the object of its prototype properties, so traversal is best Object.prototype.hasOwnProperty method to judge.

3, in the instance of the definition of the existing methods in the prototype, browser for in the situation is inconsistent. The code is as follows:

var B = {"Name": "Txj"}
b.tostring = function () {alert (' I am toString ')}
for (var key in B) {
Alert (key + ":" + B[key])
}

We add a prototype to the B instance to the existing method ToString. Modern browsers can recycle out of ToString low version browsers but not. So when you give an instance a property name, don't agree with the prototype.

4, the browser loop out the order of the properties are different. The code is the same as in 2:

Object.prototype.test = "I am Test"
var B = {"Name": "Txj"}
for (var key in B) {
Alert (key + ":" + B[key])
}

The modern browser loops through the properties in the instance, and then loops through the properties in the prototype. The lower version of the browser is the opposite.

This reminds me of a piece of code that jquery implemented for the $.isplainobject () method:

OWN properties are enumerated firstly, so to speed up,
If is own, the then all properties are own.
var key
for (key in obj) {}
return key = = = undefined Hasown.call (obj, key);

It says here that if the last attribute of an object is an instance of its own property, then all attributes are the instance's own properties. This should be wrong for a lower version of the browser. So jquery later added the following code fixes:


Support:ie<9
Handle Iteration over inherited properties before own properties.
if (support.ownlast) {
for (key in obj) {
return Hasown.call (obj, key);
}
}



Finally want to spit the groove, generally do not add a custom method on the prototype; the loop of an array is generally not used for. Ha ha!



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.