1.1 let和const是什么?
- 用来声明变量或声明常量
- let代替var,声明变量
- const声明常量constant
- 什么是变量,什么是常量?
- var、let 声明的就是变量,变量一旦初始化之后,还可以重新赋值
- const声明的就是常量,常量一旦初始化,就不能重新赋值了,否则就会报错
var username = 'Alex'
let age = 18;
const sex = 'male'
console.log(username,age,sex);
username = 'ZS'
age = 28;
console.log(username,age);
sex = 'female'
1.2 const
- const就是为了那些一旦初始化就不希望重新赋值的情况设计的
- const 的注意事项:
- 使用const声明常量,一旦声明,就必须立即初始化,不能留到以后赋值
- const声明的常量,允许在不重新赋值的情况下修改它的值
- 不知道用什么的时候就用const
let sex = "male";
sex = "famale"
console.log(sex);
const sex = "male";
sex = "famale"
console.log(sex);
const sex;
const sex = 'male'
const sex = "male"
sex = "female"
const person = {username:'Alex'};
person.username = 'ZhangSan';
console.log(person);
1.3 let、const、var的区别
- 重复声明
- 已经存在的变量或常量,又声明了一遍
- var允许重复声明,let、const不允许
var a = 1;
var a = 2;
console.log(a);
let a = 1;
let a = 2;
console.log(a);
function func(a) {
let a = 1;
func();
}
- 变量提升
- var 会提升变量的声明到当前作用域的顶部
- 养成良好的编程习惯,对于所有的变量或常量,做到先声明,后使用
var a;
console.log(a);
a = 1;
console.log(a);
console.log(a);
let a = 1;
- 暂时性死区
- 只要作用域内存在let、const,他们所声明的变量或常量就自动"绑定"这个区域,不再受到外部作用域的影响
- let、const存在暂时性死区
let a = 2;
function func(){
console.log(a);
let a = 1
}
func();
- window 对象的属性和方法
- 全局作用域中,var声明的变量,通过function声明的函数,会自动变成window对象的属性或方法
- let、const不会
var age = 18;
function add(){}
console.log(window.age);
console.log(window.add === add);
let age = 18;
const add = function(){}
console.log(window,age);
console.log(window.add === add);
1.4 let、const的应用
<button class="btn">0</button>
<button class="btn">1</button>
<button class="btn">2</button>
const aBtn = document.querySelectorAll(".btn")
for(var i = 0;i<aBtn.length;i++){
aBtn[i].addEventListener('click'),
function(){
console.log(i);
},
false
}
1.5 块级作用域
for(var i=0;i<3;i++){
}
console.log(i);
for(let i = 0;i<3;i++){
}
console.log(i);
- 作用域链(内层作用域=>外层作用域=>全局作用域)
function func(){
for(let i = 0;i<3;i++){
console.log(i);
}
}
func();
console.log(i);
{
let age = 18;
console.log(age);
}
console.log(age);
2.1 模板字符串
const username1 = 'alex'
const username2 = `alex`
console.log(username1,username2);
- 模板字符串与一般字符串的区别(和其他东西一起使用的时候,使用模板字符串,方便注入)
const person = {
username:'Alex',
age:18,
sex:'male'
}
const info = `我的名字是:${person.username},性别:${person.sex},今年${person.age}岁了`
console.log(info);
2.2 模板字符串的注意事项
const info = "第一行\n第二行"
console.log(info);
const info = `第一行
第二行`
console.log(info);
模板字符串中,所有的空格,换行或缩进都会被保留在输出之中
const info = `\`\\\'`
console.log(info);
const username = 'alex'
const person = {age:18,sex:'male'}
const getSex = function(sex){
return sex === 'male'?'男':'女'
}
const info = `${username},${person.age},${getSex(person.sex)}`
console.log(info);
2.3 模板字符串的应用
<p>学生信息表</p>
<ul id="list">
<li style="list-style: none;">信息加载中</li>
</ul>
const student = [
{
username:'Alex',
age:18,
sex:'male'
},
{
username:'Zhangsan',
age:28,
sex:'male'
},
{
username:'LiSi',
age:20,
sex:'female'
},
]
const list = document.getElementById("list");
let html = '';
for(let i = 0;i<student.length;i++){
html += `<li>我的名字是:${student[i].username},${student[i].sex},
${student[i].age}</li>`
}
list.innerHTML = html;
3.1 箭头函数
const add = (x,y)=>{
return x + y
}
console.log(add(1,1));
- 箭头函数的结构
- const/let 函数名 = 参数 => 函数体 ()=>{}
- 如何将一般函数改写成箭头函数
function add(){}
const add = function(){}
const add = function(){}
const add = ()=>{}
3.2 箭头函数的注意事项
const add = (x)=>{
return x + 1
}
const add = ()=>{
return 1 + 1;
}
console.log(add());
const add = (x,y)=>{
return x + y;
}
console.log(add(1,1));
- 单行函数体(单行函数体可以同时省略{}和return)
const add = (x,y)=>{
return x + y;
}
const add = (x,y) => x + y
console.log(add(2,1));
const add = (x,y) => {
const sum = x + y;
return sum;
}
const add = (x,y) => {
return {
value:x+y
}
}
- 如果箭头函数返回单行对象,可以在{}外面加上(),
- 让浏览器不再认为那是函数体的花括号
const add = (x,y) => ({
value:x+y
})
console.log(add(1,7));
3.3 this指向1
console.log(this);
function add(){
console.log(this);
}
add()
function add(){
console.log(this);
}
const calc = {
add:add
};
calc.add()
document.onclick = function(){
console.log(this);
}
document.onclick();
function Person(username){
this.username = username;
console.log(this);
}
const p = new Person('Alex')
- 只有在函数调用的时候this指向才确定,不调用的时候,不知道指向谁
- this指向和函数在哪调用没关系,只和谁在调用有关系
3.4 this指向2
- 箭头函数中的this指向: 箭头函数没有自己的this
const calcn = {
add:()=>{
console.log(this);
}
}
calcn.add()
function add(){
console.log(this);
}
const calc = {
add:add
}
calc.add()
const calc = {
add:function(){
const adder=() =>console.log(this);
adder()
}
}
calc.add()
3.5 不适用箭头函数的场景
const Person = () => {};
new Person();
document.onclick = function(){
console.log(this);
}
document.addEventListener('click',()=>{
console.log(this);
},false)
function add(){
console.log(arguments);
}
add(1,2,3,4,5);
const add = () => {
console.log(arguments);
}
3.6 箭头函数的应用
<button id="btn">开始</button>
<span id="result">0</span>
const btn = document.getElementById("btn");
const result = document.getElementById("result");
const timer = {
time : 0,
start:function(){
btn.addEventListener('click',()=>{
setInterval(()=>{
console.log(this);
},1000)
},false)
}
}
timer.start();