Only the expando attribute of the JScript object is traversed.

Source: Internet
Author: User
We know that JScript objects (not just objects) can add arbitrary custom attribute values like html element objects. That is to say, JScript objects are inherently Key/Value map objects. In addition, this map structure is implemented using Native code and retrieval efficiency is very high. I have discussed it in this article. However, when traversing such a map, the expando import attribute is confused with the prototype import attribute.

Because the prototype feature of JScript is very convenient for object expansion, we generally use the prototype feature to add methods for objects when creating some JScript class libraries, for example, we perform the following prototype extension on the Object:

Object. prototype. Clone = function (){};
Object. prototype. Call = function (){};
Object. prototype. OtherMethod = function (){};

At this time, if we use the Object as the map structure, we will encounter an error of traversing the map. refer to the following code:

Var objMap = {};
ObjMap ['abc'] = '1. abc ';
ObjMap ['def '] = '2. def ';
ObjMap ['ghi'] = '3. ghi ';
ObjMap ['jkl '] = '4. jkl ';

Traverse this set:

Function DisplayMap (map)
{
Var values = [];
For (var key in map)
{
Values. push (map [key]);
}
Return values;
}
Display (objMap );

We found that the values in values are: function () {}, 1.abc, 2.def, 3.ghi, 4.jkl. Really depressing! In fact, this is the effect of the for in statement. JScript is designed in this way, and we cannot change it. Can I retrieve only the attributes of expando in objMap?

Because the prototype attribute has a high priority, when the object instance is generated, it is expand to the object instance. Therefore, any object we create will contain the same prototype attribute. In this way, we can filter out the prototype attribute in objMap. The reference code is as follows:

Function GetExpandoValues (map)
{
Var values = [];
Var obj = new map. constructor ();
For (var key in map)
{
If (obj [key]! = Map [key])
{
Values. push (map [key]);
}
}
Return values;
}
GetExpandoValues (objMap );

The result is: 1.abc, 2.def, 3.ghi, 4.jkl.


Display All Expand Attributes Display Expando Attributes

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.