Some small Functions

Source: Internet
Author: User

I haven't written any logs for a long time
On the one hand, the new company can only access the Internet for one and a half hours at noon.
It will erase the interest of writing logs...

I have done something about myself recently, and I have written a lot of self-feelings. Some JS small functions are based on JQ of course.
Let's talk about the comments from the experts.

I. Provides templates to completely separate JSON HTML JS

<script type="text/template" id="template"><div class="item"></div></script>

VaR DATA = {IMG: "123.png"}; VaR _ template_str = $ (" # template "), _ STR = ""; // return a filled HTML code supporting any JSON arrangement and combination _ STR = fill_json (data, _ template_str); function fill_json (fill_json, template_str) {var JSON = fill_json, _ STR = template_str.split ("="), _ Len = _ Str. length, STR = {}; for (VAR I = 0; I <_ Len; I ++) {STR [_ STR [I] = _ STR [I] ;}; for (VAR A in Str) {If (JSON [a]) STR [a] = JSON [a] ;}; _ STR = []; for (var a in Str) {_ Str. push (STR [a]);}; return _ Str. join ("");};

The separation of HTML and JS has been written a long time ago. This is an improved version, and its performance has improved a lot compared with the previous ones.

II. Modular code loading is not so much a small function as a central architecture mechanism.
In fact, the overall performance of the system has not been improved by an order of magnitude, but the interpretation of modular project components is very good.
Core code:

VaR input = {}; // Save the imported template var input_url = {// AAA for short, "template/aaa.html". The specific storage address is AAA: "template/aaa.html", BBB: "template/bbb.html",}; ajax_template ("AAA", "click"); ajax_template ("BBB", "hover "); /*** get the corresponding clicked module * template_id the element ID name starting from each module * type trigger event click hover... **/function ajax_template (template_id, type) {VaR _ id = template_id, _ type = type; Switch (_ type) {Case "click ": $ ("#" + _ id ). unbind ("click "). Click (function () {x () ;}); break; Case "hover": $ ("#" + _ id ). unbind ("hover "). hover (function () {x () ;}); break ;}; Function x () {If (! Input [_ id]) {window. setTimeout (function () {x ();}, 100); $. post (input_url [_ id], function (result) {VaR _ result = result; $ ("body "). append (_ result); EVAL (_ ID + "_ start ();"); input [_ id] = true ;});} else {eval (_ ID + "_ start ();");};};},

Template/aaa.html page:

<SCRIPT type = "text/JavaScript" id = "aaa_js"> function aaa_start () {var OBJ = NULL; var tool = {init: function () {console. log ("loaded successfully !! ") ;}}; Tool. INIT () ;}; </SCRIPT>

This was actually applied to my project for a long time. In the past few days, I accidentally saw the dojo stuff and found that this product seems to be the same architecture idea.
What's beyond your imagination ....

After writing JavaScript for so long... The more I write, the more I use it. the more difficult it is.
Now there is a feeling that using js to write RIA is like building a house.
JS is just a tool for building bricks.
You have to burn all kinds of bricks, use tiles, walls, and so on.
Then, building a brick together in the place where it should be, and building a house slowly
However, it is really difficult to manage what bricks are used.
Stability, high performance, independent modularization, and appropriate backward compatibility...
These things can only be deeply understood by yourself in various projects.
Study every day ....

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: 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.