1:js has a problem with the loss of precision when doing decimal operations (other languages are also), such as:
0.1+0.2//0.30000000000000004
A quick way to do this is to multiply the decimal number by 10, and then calculate, for example:
(0.1*10+0.2*10)/10//0.3
But this method is not 100% accurate, multiplied by the whole can also be lost precision, such as:
2177.74*100//217773.99999999997
A, the decimal into a string, split the integer and fractional parts of the calculation, and then the results are spliced;
b, first multiply by 10, and then round with tofixed, so as to ensure that the results are accurate, such as:
The download property of the 2:<a> tag can be used to set the file name after the user has downloaded it, such as:
<a href= "Xxx.jpg" download= "renamed file, JPG"/>
But one thing to note is that files cannot cross domains, and renaming fails if the download file is in a different domain than the current domain.
3: When using instanceof to determine the object type across an IFRAME, it will fail. For example, we define an array on the parent page
var arr = [n/a];
The Arr object for the parent page is then accessed in the IFRAME:
Console.log (Parent.arr); [1,2,3]console.log (Parent.arr instanceof Array); False
The reason is that different windows have a set of their own host objects, the parent window's array constructor is not the same as the child window's array constructor, that is, arr is an array instance of the parent window, not an array instance of the child window.
That's why we do this when we judge arrays:
Object.prototype.toString.call (Parent.arr); [Object Array]
4: We use the Json.stringify method to format the JSON object. In fact, this stringify method also receives the third parameter, which supports the addition of a formatted string, such as:
Json.stringify (obj, undefined, 4);
The results are as follows:
{ "num ": 1234," str": "string", " Arr ": [1, 2, 3, 4, 5, 6], "obj": {"name": "Tom", "age": < Span class= "Hljs-number" >10, "like": [ "A", "B"]}}
Yes, it's a string. If you use a regular match, the attribute/value is added with a different highlight color, then a simple JSON data display function is available:
The 5:js Date object is automatically evaluated for the month and common year leap year judgments, which we can use to do some tricks.
When we want to calculate the number of days of a month, we often first determine whether the current month is 30 or 31 days, but also to determine if it is a leap year. In fact, calculate July 2016 How many days, can write like this:
New Date (7, 0). GetDate (); 31
JS month starts from 0, here we actually ask for is 0 days, will get July 31, also got 31 this days.
Similarly, to calculate the number of days after 20 days, you do not have to judge the month and leap year. For example, a date of 20 days is calculated today:
New Date (6, 20+20);//wed 00:00:00 gmt+0800
Got the August 9. See, the date there is more than 31st, JS will automatically help you calculate to next month.
Tips for several front ends