es6 的模板字符串,简化对象写法,箭头函数,箭头函数实践案例等:
Ⅰ、模板字符串:声明,作用
其一、代码为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>05_模板字符串</title>
</head>
<body>
<script>
// ES6 引入新的声明字符串的方式 『``』 '' "" (而在 es5 中声明字符串的两种方式:'' 和 "")
//1. 声明
// let str = `我也是一个字符串哦!`;
// console.log(str, typeof str);// 我也是一个字符串哦!, string
//2. 内容中可以直接出现换行符(模板字符串的特性之一, 而直接出现换行符在 es5 中会报语法错误)
let str = `<ul>
<li>沈腾</li>
<li>玛丽</li>
<li>魏翔</li>
<li>艾伦</li>
</ul>`;
//3. 可以变量拼接(模板字符串的特性之二)
let lovest = '魏翔';
let out = `${lovest}是我心目中最搞笑的演员!!`;
console.log(out);// 魏翔是我心目中最搞笑的演员!!
</script>
</body>
</html>
其二、结果为:
// 一进入页面后的页面:

Ⅱ、简化对象写法:大括号内可直接写入变量和函数
其一、代码为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>06_简化对象写法</title>
</head>
<body>
<script>
//ES6 允许在大括号里面,直接写入变量和函数,作为对象的属性和方法。
//这样的书写更加简洁
let name = '想回家';
let change = function(){
console.log('我们可以改变你!!');
}
const school = {
name,// 完整写法为:name: name
change,// 完整写法为:change: change
improve(){// 完整写法为:improve:function(){ console.log("我们可以提高你的技能"); }
console.log("我们可以提高你的技能");
}
}
console.log(school);// {name: '想回家', change: ƒ, improve: ƒ}
</script>
</body>
</html>
其二、结果为:
// 一进入页面后的页面:

Ⅲ、箭头函数:3个特性
其一、代码为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>07_箭头函数</title>
</head>
<body>
<script>
// ES6 允许使用「箭头」(=>)定义函数。
//声明一个函数
// let fn = function(){
// }
// let fn = (a,b) => {
// return a + b;
// }
//调用函数
// let result = fn(1, 2);
// console.log(result);// 3
//1. this 是静态的, this 始终指向函数声明时所在作用域下的 this 的值(箭头函数相比普通函数声明的区别:箭头函数的特性之一)
// 箭头函数并没有自己的 this(即:不存在指向构造函数中的实例对象), 不存在谁来调函数 this 就指向谁,通过 call 或 apply 也不会改变箭头函数中 this 的指向;
function getName(){
console.log(this.name);
}
let getName2 = () => {
console.log(this.name);
}
//设置 window 对象的 name 属性
window.name = '想回家';
const school = {
name: "XIANGHUIJIA"
}
//直接调用
// getName();// 想回家, 普通函数声明的直接调用,this 指向 window;
// getName2();// 想回家, 箭头函数由于是在全局作用域下声明的,因此 this 也指向 window
//call 方法调用(call 方法可以改变函数内部 this 的值)
// getName.call(school);// XIANGHUIJIA(即:普通函数可以通过 call 改变 this 的指向)
// getName2.call(school);// 想回家(即:箭头函数不能通过 call 改变 this 的指向)
//2. 不能作为构造实例化对象(箭头函数相比普通函数声明的区别:箭头函数的特性之二)
// let Person = (name, age) => {
// this.name = name;
// this.age = age;
// }
// let me = new Person('xiao',30);// // 会报错:Uncaught TypeError: Person is not a constructor
// console.log(me);
//3. 不能使用 arguments 变量(箭头函数相比普通函数声明的区别:箭头函数的特性之三)
// let fn = () => {
// console.log(arguments);
// }
// fn(1,2,3);// 会报错:Uncaught ReferenceError: arguments is not defined
//4. 箭头函数的简写
//1) 省略小括号, 当形参有且只有一个的时候
// let add = n => {
// return n + n;
// }
// console.log(add(9));// 18
//2) 省略花括号, 当代码体只有一条语句的时候, 此时 return 必须省略
// 而且语句的执行结果就是函数的返回值
let pow = n => n * n;
console.log(pow(8));// 64
</script>
</body>
</html>
其二、结果为:
// 一进入页面后的页面:

Ⅳ、箭头函数实践案例:背景切换和数组过滤
其一、代码为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>08_箭头函数实践</title>
<style>
div {
width: 200px;
height: 200px;
background: #58a;
}
</style>
</head>
<body>
<div id="ad"></div>
<script>
//需求-1 点击 div 2s 后颜色变成『粉色』
//获取元素
let ad = document.getElementById('ad');
//绑定事件
ad.addEventListener("click", function(){
// //保存 this 的值(即:针对普通定时函数的 this 指向问题的解决方案,之前的解决方案一:)
// let _this = this;// 此时是将外层函数作用域下的 this 保存为 _this;
// setTimeout(function() {
// console.log(this)// 此时的 this 指向的是 window
// // this.style.background = 'pink';// 会报错:Uncaught TypeError: Cannot set properties of undefined (setting 'background')(报错原因: window 上没有 style 属性值)
// _this.style.background = 'pink';// 此时的 _this 的指向就是外城函数作用域下的 this,此时的 _this 指向才没有问题;
// }, 2000)
//定时器(解决方案二:)
setTimeout(() => {
//修改背景颜色 this(也可以通过 div 来做)
// console.log(this);// <div id="ad"></div>
this.style.background = 'pink';// 此时 this 能成功使用的原因:箭头函数下的 this 是静态的,指向是声明时所在作用域下的 this 的值,因为在外层 function(){} 中的 this
// 指向的是事件源 ad,因此就可以找到 ad 元素并可以修改其背景色;
}, 2000);
});
//需求-2 从数组中返回偶数的元素
const arr = [1,6,9,10,100,25];
// const result = arr.filter(function(item){// 使用原来函数声明方式来获取结果的过程;
// if(item % 2 === 0){
// return true;
// }else{
// return false;
// }
// });
const result = arr.filter(item => item % 2 === 0);
console.log(result);// [6, 10, 100]
// 箭头函数适合场景的总结:
// 箭头函数适合与 this 无关的回调场景(如:定时器, 数组的方法回调等)
// 箭头函数不适合与 this 有关的回调场景(如:事件回调, 对象的方法)
</script>
</body>
</html>
其二、结果为:
// 一进入页面后的页面:

// 点击 #58a 背景的元素框后,2s 后会变成 pink;

Ⅴ、小结:
其一、哪里有不对或不合适的地方,还请大佬们多多指点和交流!
其二、若有转发或引用本文章内容,请注明本博客地址(直接点击下面 url 跳转) https://blog.csdn.net/weixin_43405300,创作不易,且行且珍惜!
其三、有兴趣的话,可以多多关注这个专栏(Vue(Vue2+Vue3)面试必备专栏)(直接点击下面 url 跳转):https://blog.csdn.net/weixin_43405300/category_11525646.html?spm=1001.2014.3001.5482
其四、再有兴趣的话,也可以多多关注这个专栏(Java)(直接点击下面 url 跳转):https://blog.csdn.net/weixin_43405300/category_12654744.html?spm=1001.2014.3001.5482
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/weixin_43405300/article/details/171106093




