-use = = = instead of = =
the = = (or! =) operator automatically performs type conversions when needed. the = = = (or!==) operation does not perform any conversions. It compares values and types, and is considered better at speed than = =.
--using closures to implement private variables
function fn1 () { var pro1= "a"; This.fn2=function () {return pro1;}}
3-Get a random item from the array
<script> var items = [548, ' a ', 2, 5478, ' foo ', 8852,, ' Doe ', 2145, 119,11]; var randomitem = Items[math.floor (Math.random () * items.length)]; </script>
4– get a random number within a specific range
This code snippet is useful when you want to generate test data, such as a random salary value between the minimum and maximum values.
var x = Math.floor (Math.random () * (Max-min + 1)) + min;
5– generates a random numeric alphabetic string
function Generaterandomalphanum (len) { var rdmstring = ""; for (; rdmstring.length < Len; rdmstring + = Math.random (). toString (. substr (2)); return rdmstring.substr (0, Len);}
6 – Scramble a numeric array
var numbers = [5, 458, -215, 228, 122205, -85411];numbers = Numbers.sort (function () {return math.random ()- 0.5});/* The array numbers would be equal for example to [5, 228, -215, 458, 122205, -85411,] */
Trim function of 7–string
There is a classic trim function in Java, C #, PHP, and many other languages to remove the whitespace from the string, not in JavaScript, so we need to add this function to the string object.
String.prototype.trim = function () {return this.replace (/^\s+|\s+$/g, "");};
8– verify whether the parameter is a number
function Isnumber (n) { return!isnan (parsefloat (n)) && isfinite (n);}
9– Verify if the parameter is an array
10– gets the maximum or minimum value in a numeric array
var = [5, 458, -215, 228, Numbers, 122205, -85411];var maxinnumbers = Math.max.apply (Math, numbers); var Mininnumbers = Math.min.apply (Math, numbers); "Note: Here are the techniques for passing parameters using the Function.prototype.apply method"
11– empty An array
12– do not use Delete to delete an item in an array.
Use splice instead of Delete to remove an item from the array. Using delete simply replaces the original item with undefined, not the actual deletion from the array. The Delete method should be used to delete a property of an object.
13– rounding a number, preserving n decimal places
Str.tofixed (2);
14– floating point problem
Why is that? 0.1+0.2 equals 0.30000000000000004. You know, all of the JavaScript numbers are internally floating numbers in 64-bit binary notation, conforming to the IEEE 754 standard. For more information, you can read this blog post. You can use the toFixed () and Toprecision () methods to solve this problem.
15– checking properties when using For-in to traverse an object's internal properties
for (var name in object) { if (Object.hasownproperty (name)) { //does something with name }}
16– comma operator
var a = 0;var B = (a++); Console.log (a); A'll is equal to 1console.log (b); B is equal to 99
-JSON-based serialization and deserialization (serialization and deserialization)
var person = {name: ' Saad ', age:26, department: {id:15, Name: "R & R"}};var Stringfromperson = Json.stringify (per son);/* Stringfromperson is equal to ' {' name ': ' Saad ', ' age ': ', ' Department ': {' ID ': ' Name ': ' R '}} ' */var personfromstring = Json.parse (Stringfromperson);/* personfromstring is equal to Person object */
18– Pass in the function when calling SetTimeout () and setinterval (), not the string.
If you pass the string to SetTimeout () or setinterval (), the string will be parsed as if it were using eval, which is very time consuming.
Do not use:
SetInterval (' dosomethingperiodically () ', +), SetTimeOut (' Dosomethingafterfiveseconds () ', 5000);
And with
SetInterval (dosomethingperiodically), SetTimeOut (Dosomethingafterfiveseconds, 5000);
-Keep in mind that primitive operators are always more efficient than function calls.
Do not var min = Math.min (A, b); A.push (v);//var min = a < b? A b; A[a.length] = v;
Javascript Programming Tips Summary (part of the content for reference to others)