概述
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];
最后
以上就是繁荣小笼包为你收集整理的JavaScript的ES6语法,2021-03-04的全部内容,希望文章能够帮你解决JavaScript的ES6语法,2021-03-04所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复