As the front-end basic work will be used every day JS ... But do we really know about JS, or do we have any tips that we don't know about?
So.. Here are a few tips on JS .....
One: Timer (multiple parameters can be passed)
The first is a general timer, generally we use the Timer 2 parameters, one is a function, one is time.
SetTimeout (function () { //Functions alert (1);},2000) //Time
But in fact the timer has a number of parameter parameters, tips:
SetTimeout (num) { //pass a formal parameter alert (num);},2000,123)//callback parameter
Two: Stitching strings
The problem of stitching strings is often encountered at work, such as when we add content to a BODY element:
Window.onload=function () {document.body.innerhtml= ' <div>div</div><span>span</span></ P>p</p>123 ';}
When these strings are on a single line, this adds no problem, and if we add too much content to this line, this is usually the case:
Window.onload=function () {document.body.innerhtml= ' <div>div</div> ' + ' <span>span</span> ' + ' </p>p</p> ' + ' 123 ';}
This universal approach is duly completed and there is no problem.
Here's a tips that might not be so common:
Window.onload=function () {document.body.innerhtml= ' <div>div</div>\ //After character plus a backslash <span> Span</span></p>p</p>123 ';}
The same, the wood is a bit of a problem!
Three: Console.log ()
Mainly used for printing, debugging information, first, in the Debug console output a Hallo
<script>var a = ' Hallo '; Console.log (a);</script>
OK, no problem, in fact, we can not only output a character, but also output image, style. Now let's add a style to this hallo.
<script>var a = ' Hallo '; Console.log ('%c ' +a, ' font-size:400%;background:blue;color:red; '); </script>
Very reliable ~
Four: tipeof
This is usually used to detect the type of data, which is generally used
var a = [];alert (typeof a) //Connect with a space, popup object
You can use it.
var a = [];alert (typeof (A))//with () connection, Popup object
V: Nested for loop
First define 2 for loops
for (Var i=0;i<5;i++) { //i loop for (Var j=0;j<1;j++) { //j loop if (i==3) {break; Did not jump out of the I loop, just skipped 3.} alert (i);}}
Sometimes we need to jump out of the big loop so
A:for (var i=0;i<5;i++) { //give loop a name for (Var j=0;j<1;j++) {if (i==3) {Break A; Jump out of this loop}alert (i);}}
Six: For Loop
1. Most commonly used
for (Var i=0;i<5;i++) { //3 value alert (i)}
2. Also commonly used
var i = 0; Externally defined IFOR (; i<5;i++) {//2 values Alert (i)}
3. Infrequently used
var i = 0;for (;;) { //0 Values alert (i) if (++i>=5) {// need to be judged, otherwise dead loop break;}}
Seven: Call
var obj = { a:function () {alert (this)}}var arr = [1,2,3];obj.a (); This points to obj
If you want to make this point to arr, just call (arr)
Obj.a.call (arr);
If call () is not pointing, it points to window automatically.
Eight: anonymous function self-executing
Wrong wording
function () {
Correct notation Plus (parentheses)
(function () { alert (123);}) (); Automatically execute alert ();
If you don't want to add parentheses and you want to do it yourself, you can
!function () {alert (123);} ();
Plus operator, also will not error, not only can add!, can also ~, or + (all do not error)
Nine: Creating objects
So so
var arr = new Array (); alert (arr.length);
In fact, can not use the object behind the ();
var arr = new Array;alert (arr.length);
In fact, are a lot of small and small details, write less, do more, how happy a thing ha!
Finally, the details determine success or failure! \ 0.0/
A few tips on JS