十个JS技巧

1、定时器

setTimeout( function( num){
alert( num);
}, 1000, 123);
定时器不只有两个值,123是前面函数的回调参数

2、拼接字符串

document. body. innerHTML =
'
div
\
span \

p

\
55555';

可以用“\”拼接字符串

3、console.log()

var a = 'hello';
console. log( '%c' + a, 'font-size:400%;background:blue;color:white;');
可以在console.log里加样式

4、typeof

var arr = [];
arr. num = 10;
// alert(typeof arr);
// alert(typeof(arr));
//instanceof
//in
// alert(arr instanceof Object);
// alert(arr instanceof(Object));

// alert('num' in arr);
alert( 'num' in ( arr));
typeof arr 等同于 typeof(arr);

5、嵌套for循环

a: for( var i = 0; i < 5; i ++){
for( var j = 0; j < 1; j ++){
if( i == 3){
break a;
}
alert( i);
}
}
跳出循环

6、for(;;)

var i = 0;
for(;;){
if( ++ i >= 5){
break;
}
}

7、Call

var obj ={
aaa: function(){
alert( this);
}
};
var arr = [ 1, 2, 3];
obj. aaa. call();

8、insertBefore

type= "button" value= "添加" id= "input1" >
id= "ul1" >


window. onload = function(){
var oInput = document. getElementById( 'input1');
var oUl = document. getElementById( 'ul1');
var iNow = 0;
var aLi = oUl. getElementsByTagName( 'li');

oInput. onclick = function(){
var oLi = document. createElement( 'li');
oLi. innerHTML = iNow ++;
oUl. insertBefore( oLi, aLi[ 0]);
};
};
oUl.appendchild()是在后面添加子节点,insertBefore是在前面添加

9、匿名函数自执行

( function(){
alert( 123);
})();
~ function(){
alert( 123);
}();
! function(){
alert( 123);
}();

     + function(){
alert( 123);
}();

用括号把匿名函数括起来,或者在前面添加位运算符,匿名函数才能自执行,不报错

10、创建对象

function Aaa(){}
var a1 = new Aaa;
alert( a1);
var arr = new Array;
alert( arr);
创建对象时可以省略()

你可能感兴趣的:(十个JS技巧)