Simulation of common java classes in JS Advanced Programming

Source: Internet
Author: User

Preface

Have you ever encountered this situation: You want to splice a long string during javascript programming. If you still use the "+" Operator for processing, it means you are really amateur; maybe you will think that there is a class called StringBuilder in java specifically used to process String concatenation. Is there a similar class in javascript? Unfortunately, javascript does not provide similar classes.



It is the most important thing to think of using StringBuilder. if Javascript is not provided, we can write one by ourselves. Then, store the StringBuilder class in your own js code library (A. js file). In the future, use similar functions to directly introduce a js file.

Js is a very flexible language. We can use native js to simulate many classes in java, and then put these "classes" (actually functions) into one. you can use the js file as your own code library.

Next I will give you some examples to illustrate how to use js to simulate common java classes.


Practice


StringBuilder

/* StringBuilder String concatenation class */function StringBuilder () {this. str_array = new Array ();} // Add the append method StringBuilder for it. prototype. append = function (data) {this. str_array.push (data); return this;} // Add the toString method to StringBuilder. prototype. toString = function () {return this. str_array.join ('');}

Example

Var builder = new StringBuilder (); var name = "wh"; builder. append ('this blog is '). append (name ). append ('write'); alert (builder. toString ());


HashMap

/* HashMap class */function HashMap () {this. entryArray = new Array ();} // put function HashMap. prototype. put = function (key, value) {var obj = this. get (key); if (obj = null) {this. entryArray. push ({key: key, value: value, getKey: function () {return key ;}, getValue: function () {return value ;}});} else {for (var index in this. entryArray) {if (this. entryArray [index]. key = key) {this. entryArray [index]. value = value ;}}// get function HashMap. prototype. get = function (key) {var value = null; for (var index in this. entryArray) {if (this. entryArray [index]. key = key) {value = this. entryArray [index]. value; break ;}} return value ;}// clear function HashMap. prototype. clear = function () {this. entryArray = new Array ();} // putAll function HashMap. prototype. putAll = function (map) {if (map instanceof HashMap) {for (var index in map. entryArray) {this. put (map. entryArray [index]. key, map. entryArray [index]. value) ;}}// entrySet function HashMap. prototype. entrySet = function () {return this. entryArray;} // keySet function HashMap. prototype. keySet = function () {var keyArray = new Array (); for (var index in this. entryArray) {keyArray. push (this. entryArray [index]. key);} return keyArray;} // values Function HashMap. prototype. values = function () {var valueArray = new Array (); for (var index in this. entryArray) {valueArray. push (this. entryArray [index]. value);} return valueArray ;}


We can see from the above

Use [], {}, and other existing data structures to store data. The json data structure formed by [] + {} can simulate various data structures;

Define a function as a "class", such as StringBuilder, which requires a Data Structure to store data.

Add methods on the prototype object of the class, such as append

It's that simple. Have you learned it?


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.