How JavaScript creates Dictionary objects

Source: Internet
Author: User

For JavaScript, its own array object is only a number of arrays, can not provide through the keyword to get saved data, jquery source provides a very good way to solve this problem, first look at the source code:

function Createcache () {var keys = [];function cache (key, value) {//Use (key + "") to avoid collision with native Protot ype//properties (see Issue #157) if (keys.push (key + = ") > Expr.cachelength) {//Only keep the most recent Entriesdel Ete Cache[keys.shift ()];} Return (Cache[key] = value);} return cache;}
The above source is to create a compilation of the results of the cache, the code is called the following way:

var codecache = Createcache ();
In the source code, keys is used to hold the key, and the cache object is used to hold the key value pair, and the maximum number of control keys is expr.cachelength by the global variable, and the first key and the key value pair are automatically deleted if the number is exceeded.

This code leverages the structure of the closure so that external code cannot access the keys variable, which ensures the security of the keys variable. Of course, because of the characteristics of JavaScript statements, external code can also make the key and key-value pairs do not match by modifying the cache property. However, as long as not intentionally spoof, this itself should not have too much relationship.

Of course, it is also not possible to pledge a perfect Dictionary object, because it does not provide key functions such as primary key repetition judgment, and interested friends can perfect it.



How JavaScript creates Dictionary objects

Related Article

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.