基本知识
字符串
let num = 100 ;
console.log(`abc${num}def`);
解构
var obj = {nme:'tom' , age:19}
function fn( {nme,age}){
console.log(nme,age);
}
fn( obj )
var arr = ['abc',12,'文字'] ;
var arrs = [...arr] ;
console.log(arrs); //['abc',12,'文字']
类数组
function fn( a , b , ...args ){
console.log(a);
console.log(b);
console.log(args[0]);
}
fn( 'a' , 'b' , [1, 2, 3] )
函数参数默认值
function fn( a=1 , b=2 ){
console.log( a+b )
}
fn(2)
fn(3,2)
fn()
bind
function fn(){
this.i = 0 ;
setInterval(function(){
console.log( this.i++ )
}.bind(this) , 500)
}
fn() //1 2 3 4 5 6....
function fn(){
this.i = 0 ;
setInterval( ()=>{
console.log( this.i++ )
} , 500)
}
fn() //1 2 3 4 5 6....
继承
- 寄生式继承
语句与表达式
js中语句优先,可以解释成语句就不会解释成表达式
var a = ;
x是label
1是statement
逗号是表达式,结果是最后一个
{ a:1 , b:2 } 逗号后必须是表达式,这里不是所以报错
高阶函数
回调函数
var arr = [1,2,3,4,5] ;
arr.forEach(function( item ){
console.log( item )
});
闭包
function fn(){
var i = 0 ;
return function(){
return ++i ;
}
}
var f = fn() ;
f()