JavaScript的ES6语法,2021-03-04

let

let 不能重复声明

let a=0;
let a=2;   报错

// 块级作用域
{
    let a=1;
}
console.log(a);  //报错

// 不存在变量提升
console.log(a); //报错
let a= 'aa';

let [a,b,c] = 'ff';

const

// 常量,赋初始值
// 潜规则 大写  const PI ="3.14
// 不能修改,块级作用域

//对象的结构
const zhao = {
    age:18,
    hy:function(){
        
    }
}
let {a,b,hy} = zhao;
console.log(a);
hy();
//无需 使用 zhao.hy()

声明方式三种

// 1. 声明方式 '',"",``;
let str = `tea`;
console.log(str, typeof str);

// 2. 内容可出现换行
let str = `<ul><li>1</li>
			  <li>2</li>
		  </ul>`;
//3 拼接
let a = 'zhangsan';
let b = `${a} is a good boy`;

简化书写

const school = {
    name,  //等价于 name:name,
    hobby(){ }  //等价于 hobby:function(){}
}

箭头函数 =>

let fn = function(){}

let fn = (a,b) =>{}  //声明
let res = fn(1,2);
console.log(res);  //调用
// 1 this是静态,this始终指向函数声明时所在作用域下的this
function getName(){}
let getName2 = () =>{}  
window.name = 'aa';
const school = { name:"井大"}

getName();  //aa
getName2(); //aa

getName.call(school); //井大
getName2.call(school); //aa

// 2 不能作为构造器实例化对象
let Person =(name,age)=>{
    this.name = name;
    this.age = age;
}
let me = new Person('xiao',30);  //结果报错
// 3 不能使用arguments 变量
let fn=() =>{
    console.log(arguments);
}
fn(1,2,3); //报错
// 4 箭头简写
	// 省略小括号,形参只有一个
	let add = n =>{ return n+n;}
	//省略花括号,只有一条语句,return必须省略,返回结果值
    let add = n =>n*n

    
 // 箭头函数适合与this 无关的回调,,定时器,数组的方法回调
  const arr = [1,6,9,10]; //找偶数
const result = arr.filter(function(i){
    if(i %2 === 0){
        return true;
    }else{return false;}
}); 
const result2 = arr.filter(i=>i%2 === 0);

 // 箭头函数不适合与this有关的回调,,事件回调,对象的方法

函数默认参数值

//1 一般位置放最后
function add(a,b,c=1){}
//2 与解构赋值结合
connect({
    name:'localhost',
    passwd:'123'
})
function connect(op){
    let name = op.name;
} //等价于
function connect({name="127.0.0.1",passwd}){
    console.log(name)
}

rest参数:…args

//rest参数放在最后
function fn(a,b,...args){
    
}
fn(1,2,3,4,6)

一、扩展运算符 …

1 数组合并

 function chunwan(){ //放rest参数 ...XXX
 console.log(arguments);

 }
 chunwan(...tfboys); //扩展放在调用实参,

 const e = [...a,...b]; //数组合并

2 数组克隆

const a =['a','b'];
const b = [...a];

3 伪数组转真数组

const divs = document.queryselector('div');
const divArr = [...divs];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值