Sizzle.js Learning notes implementing data structures using closure simulation: Dictionary (MAP)

Source: Internet
Author: User

Sizzle.js Learning notes implementing data structures using closure simulation: Dictionary (MAP)

These days to learn and see the jquery and property of the two Popular Front-end library of the Queen selector component Sizzle.js source code, harvest or relatively many! has been done using the Java language development, its rich component class Library makes development efficiency that's a quick one! It's a bit out of the blue to turn to JavaScript for a while (almost half a year), but since seeing so many beautiful websites and getting in touch with JavaScript, you've found some of the fun. As I have always believed, programming language is just a tool, the important thing is the programming idea ! The use of JavaScript also adds a lot of understanding to Java. At the same time also feel the most basic things to use the language to achieve some common components is also a happy thing.

Today's source code learning to see the cache is the implementation of the time, has further deepened the understanding of JavaScript closures! The great god John Resig really very Diao ~ ~ His family has the time must see his book "Secrets of the JavaScript Ninja", but this book does not seem to yo Chinese version ~~~!!!

function map (length) {///If the length of the map is not specified, the default initial length is 16length = length | | 16;var keys = new Array (length); function cache (Key, Val UE) {//+ "" string after key to prevent the value of key and some local property value naming conflicts//using expression statements to ensure that the statement can correctly execute return (cache[key + ""] = value);} Returns the cache function, using the powerful closure function in JavaScript to return to cache;}
The JavaScript closure feature is used here to simulate the implementation of the map structure in Java. With closures, you can ensure that each time we operate on the same array object, not the newly opened array object. However, it is important to note that the garbage collection mechanism of JavaScript uses the reference counting scheme, which should be careful not to cause circular references on value assignment. This can easily cause a memory leak!!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sizzle.js Learning notes implementing data structures using closure simulation: Dictionary (MAP)

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.