let 的用法
都是用来定义变量的
1.必须先定义后使用
2.不能重复使用
3.块级作用域
for(let i=0;i<5;i++){
oLis[i].onclick = function(){
console.log(i);
4.暂时性死区 :就近原则:当内部变量与外部变量重名时,内部变量自动屏蔽外部变量
let a = 1
{
let a=123;
console.log(a);
}
Const
1.只能读不能写
被const修饰的变量为只读变量
const PI = 3.1415926;
PI = 666;
console.log(PI);
2.被const修饰的变量必须初始化
3.不能重复定义
4.必须先定义后使用
5.有块级作用域
不成文的规定:习惯上只读变量大写
//js中的const只能修饰栈区,不能直接修饰堆区
const arr = [1,2,3];
arr[0] = 666;//可以改
console.log(arr[0]);
const arr1 = [4,5,6];
arr = arr1;//不可以改
this:函数的内置对象
作用
1.与事件体连用,代表触发时间的元素
document.onclick = function(){
console.log(this);
2.与普通函数连用,代表调用该函数对象
function fun(){
console.log(this);
this.document.write("嘿嘿嘿");
}
window.fun();
bind是一个函数对象的方法
功能:用来改变函数的this指向,通常修饰匿名函数
参数:bind(被修改的this指向,[原函数的参数1,原函数参数2.....])
let oBox = document.querySelector("#box");
document.onclick = function(){
this.style.display = "none";
}.bind(oBox);
for......of......与for.......in.......
for......of......:遍历值
for.......in.....:遍历下标
字符串扩展方法
includes(参数):返回布尔值,表示是否找到了参数字符串
startsWith(参数):返回布尔值,参数是否在源字符串的头部
endsWith(参数):返回布尔值,参数是否在源字符串的尾部
中文的读取格式是utf-16
箭头函数:
箭头函数代替匿名函数
let fun = function(){
console.log("heihei");
}
fun();
let fun = ()=>{
console.log("heihei");
}
注意事项:
1.如果某个匿名函数只有一个参数,则可以直接省略形参的括号
let fun = a=>{
console.log(a);
}
2.如果函数体只有一行代码,则可以省略函数体的{}
let fun = a=>console.log(a)
3.如果函数体只有一行代码,则自带return
结构赋值
1.根据数据结构进行赋值
左边的数据结构要和右边一致
let [x,y,z] = [1,2,3];
console.log(x,y,z);
2.交换两个变量的值
let [a,b] = [1,2];
let t;
set集合
特点:没有下标,自动去重
let set = new set (数组);
let set = new set ([1,2,3,4,5,6,7,8,9])
add(参数)向集合中添加一个元素
delete(值)删除集合中某个数
has(值)判断集合中是否含有某个值
clear()清空集合
数组去重
let arr = [1,2,3,1,2,5,5,3,4];
let set = new Set(arr);
//Array.from将容器转换为数组
arr = Array.from(set);
console.log(arr);