ES6
一、ES6简介
二、ES6的新增语法
①、let关键字
<script type="text/javascript">
var num = 10
if (true) {
console.log(num);
let num = 20;
}
</script>
②、let的经典面试题
③、const关键字
<script>
const PI = 3.14;
const ary = [100, 200];
ary[0] = 123;
ary = [1, 2]
console.log(ary);
</script>
④、let、const、var的区别
⑤、解构赋值
<script type="text/javascript">
let ary = [1,2,3];
let [a, b, c, d, e] = ary;
console.log(a)
console.log(b)
console.log(c)
console.log(d)
console.log(e)
</script>
<script type="text/javascript">
let person = {name: 'lisi', age: 30, sex: '男'};
let {name: myName} = person;
console.log(myName)
</script>
⑥、箭头函数
<script>
function fn() {
console.log(this);
return () => {
console.log(this)
}
}
const obj = {
name: 'zhangsan'
};
const resFn = fn.call(obj);
resFn();
</script>
⑦箭头函数面试题
<script>
var age = 100;
var obj = {
age: 20,
say: () => {
alert(this.age)
}
}
obj.say();
</script>
⑧、剩余参数
<script>
let ary1 = ['张三', '李四', '王五'];
let [s1, ...s2] = ary1;
console.log(s1)
console.log(s2)
</script>
<div>1</div>
<div>4</div>
<div>3</div>
<div>6</div>
<div>2</div>
<div>5</div>
<script>
var oDivs = document.getElementsByTagName('div');
console.log(oDivs)
var ary = [...oDivs];
ary.push('a');
console.log(ary);
</script>
三、ES6的内置对象扩展
①、Array的扩展方法
②、Array.from方法
<script>
var arrayLike = {
"0": "1",
"1": "2",
"length": 2
}
var ary = Array.from(arrayLike, item => item * 2)
console.log(ary)
</script>
③、实例方法find()
<script>
var ary = [{
id: 1,
name: '张三'
}, {
id: 2,
name: '李四'
}];
let target = ary.find(item => item.id == 3);
console.log(target)
</script>
④、实例方法:findIndex()
<script>
let ary = [10, 20, 50];
let index = ary.findIndex(item => item > 15);
console.log(index)
</script>
⑤、实例方法:includes()
<script>
let ary = ["a", "b", "c"];
let result = ary.includes('a')
console.log(result)
result = ary.includes('e')
console.log(result)
</script>
⑥、模板字符串
<script>
const fn = () => {
return '我是fn函数'
}
let html = `我是模板字符串 ${fn()}`;
console.log(html)
</script>
⑦、String的扩展方法
Ⅰ、StartsWith()和endsWiths()
<script>
let str = 'Hello ECMAScript 2015';
let r1 = str.startsWith('Hello');
console.log(r1);
let r2 = str.endsWith('2016');
console.log(r2)
</script>
Ⅱ、repeat()
<script>
console.log("y".repeat(5))
</script>
⑧、Set数据结构
<script>
const s5 = new Set(['a', 'b', 'c']);
s5.forEach(value => {
console.log(value)
})
</script>
|