This log is completely inspired by reading a log. The original text is: Evil eval and new function.
We rarely use new Array to define arrays. I didn’t expect new Array to have such a wonderful use. Through new Array(n), we can create n empty elements, and there are n-1 blanks in the middle of n elements. Through join, we can put some things in the middle of these blanks to form the special string we need. This inspires me. This feature allows me to easily implement a "continuous operation of regular elements" without looping. For example, calculate the sum of values from 1-100. In the past, we needed to loop from 1 to 100 and then sum it, but using this feature of the array we can do it with a simple line of code, the code is as follows:
var i=0,sum=eval('0'+new Array(101).join('+(++i)'));
In addition, you can do many similar mathematical operations, such as the following
eval('0'+new Array(11).join('+(++i,2)')) // Calculate the sum of squares of 1-10
eval('0'+new Array(100/2).join('+(i+=2)')) //Calculate the sum of 2+4+6+...100
These calculations that we used to perform through loops can now be done cleverly with one line of code. We have to admire the implicit dexterity of programming languages. There are always too many things waiting for us to discover in the world of programming.
Change the above JavaScript code to VBS, which is the so-called ashes solution to VBS exercises:
Dim a(101)
Execute(Join(a,"s=s+i:i=i+1:"))
MsgBox s
original:/programming/
We rarely use new Array to define arrays. I didn’t expect new Array to have such a wonderful use. Through new Array(n), we can create n empty elements, and there are n-1 blanks in the middle of n elements. Through join, we can put some things in the middle of these blanks to form the special string we need. This inspires me. This feature allows me to easily implement a "continuous operation of regular elements" without looping. For example, calculate the sum of values from 1-100. In the past, we needed to loop from 1 to 100 and then sum it, but using this feature of the array we can do it with a simple line of code, the code is as follows:
Copy the codeThe code is as follows:
var i=0,sum=eval('0'+new Array(101).join('+(++i)'));
In addition, you can do many similar mathematical operations, such as the following
Copy the codeThe code is as follows:
eval('0'+new Array(11).join('+(++i,2)')) // Calculate the sum of squares of 1-10
eval('0'+new Array(100/2).join('+(i+=2)')) //Calculate the sum of 2+4+6+...100
These calculations that we used to perform through loops can now be done cleverly with one line of code. We have to admire the implicit dexterity of programming languages. There are always too many things waiting for us to discover in the world of programming.
Change the above JavaScript code to VBS, which is the so-called ashes solution to VBS exercises:
Copy the codeThe code is as follows:
Dim a(101)
Execute(Join(a,"s=s+i:i=i+1:"))
MsgBox s
original:/programming/