Home  >  Article  >  Web Front-end  >  Summarize some programming skills of two Javascript hash objects_javascript skills

Summarize some programming skills of two Javascript hash objects_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:15:01971browse

I like to start with the most basic content and then go deeper. Experts may need to be more patient.
Let’s look at the simplest application first. In the code below, we need to implement a function, which is to click each button to display the corresponding URL

Copy code The code is as follows:





So. How to write this showUrl function? I think most people might write it like this.


This way It’s not very good to write, for two reasons:
1. It’s too long and troublesome to write. Use if and switch statements to write. If there are 100 conditions, why not write 100 statements?
2. It’s not easy to maintain. And expansion, what if the requirements change frequently and the data comes from the database? Every time there is a change, the logical structure of the program must be changed
Programmers who are more experienced in JavaScript will definitely not write like this, and generally use arrays to implement it. It can be a two-dimensional array or a double array
1. Double array method
Copy code The code is as follows:



2. Two-dimensional array method
Copy code The code is as follows:



Did you see that, we had to use a loop before? Yes, you need to use a judgment function. Now you only need one line of code, and the scalability is still the best.
If you are familiar with JavaScript, you will definitely have a special liking for arrays, because it is indeed a very convenient and widely used
data structure, but for the green apple of the hash object, even if you Just take one bite of it and you will never forget its sweetness.
As a data structure, it can simplify programming in many situations. In the face of massive data, its performance is much higher than that of arrays. (This will be mentioned in
my future post, please pay attention)
As an object, it can implement classes in JavaScript and simulate object-oriented programming.
The above is very simple and is only introduced as a tip. If you are interested, you can talk about your application experience in the reply.
Before I leave, let me give you an example - determine whether the uploaded file is an image file.

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh it to execute
]
Please pay attention to the set function when we implement it After this function, you can use collection objects like python. Isn’t it very convenient?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn