Javascript A method that simultaneously declares a series of (multiple) variables

  • 2021-07-13 04:19:33
  • OfStack

Preface

js declares multiple variables at the same time. We believe that many friends will repeatedly use var to define it. If we want to achieve a more scientific method, let's look at an article about js's method of declaring variables. The specific details are as follows.

JS declaration variable method I will not say, if you need to learn friends can click on this article.

We often need to declare several variables in succession, which is what I did when I was a novice.


var a=1;
var b={};
var c=[];
var d=...

In fact, we can do this:


var a=1,b=2,c=3,d=4;

That is to say, separate multiple variables.

In this way, the case of no newline is only suitable for the case of variable value comparison of single 1. If your variable is a function or an object, it is necessary to newline:


var a=function (){
 var b,c,d;
},
b = {
 b:1,
 a:2
};

In this way, 1 can be written straight and keep good readability. Of course, remember to use it in the end; End.

In addition, I want to mention here that I have used several JS compression tools recently, all of which are very intelligent, but they are still not intelligent enough to automatically merge multiple var in multiple lines into one var. Therefore, if you want to compress your JS, it is best to use this continuous declaration method. jQuery and so on are all used in this way.

For more readers interested in JavaScript related content, please check the topics on this site: "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithms and Skills" and "Summary of JavaScript Mathematical Operation Usage"

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: