if(false){
var num = 20
let num1 = 20
}
console.log(num)//undefined
console.log(num1)//报错 块作用域
const name_1 = ''
const name_1 = ''
name_1 = "1s"
console.log(name_1)//会报错
let [x, y, z] = [10, 20, 30]
console.log(x, y, z)//10 20 30
let [m, n, l] = [40, 50]
console.log(m, n, l)//40 50 undefined
let [m, n, l = 60] = [40, 50,10]//10的优先级高
console.log(m, n, l);//40 50 10
let [q, w, [e, r, t = 't']] = ['q', 'w', ['e', 'r']]
console.log(q, w, e, r, t);// q w e r t
let str = '我们是web31期,我们都是好孩子'
let [a, b, c, d] = str
console.log(a, b, c, d);//我们是w
console.log(str[1]);//们
console.log(str.substr(1, 1));//们
console.log(str.substring(1, 2));//们
console.log(str.slice(1, 2));//们
console.log(str.charAt(1));//们
let obj = {
name: 'zs',
age: 18,
sex: '男'
}
let { name, age, sex } = obj
console.log(name, age, sex);//zs 20 男
console.log(obj.age);//20
const { age } = obj
console.log(age);//20
var { name, age = 20 } = { name: 'ls' }
console.log(name, age);//ls 20
var x = 10
var y = 20
var [y, x] = [x, y]
function fn(x, y){
return [x, y]
}
var [x, y] = fn(10, 20)
console.log(x, y)//10 20
function fn_1({ age = 20 }){
console.log(age)
}
fn_1({ age: 30 })//30
let str_1 = `我叫${person.name},我的性别是:${person.getSex()}`
console.log(str_1);//我叫zyf,我的性别是女
let str1 = "web31期"
let str2 = str1.repeat(3)
console.log(str2);//web31期web31期web31期
console.log(str1.includes("1"));//true
console.log(str1.includes("2"));//false
console.log(str1.startsWith("web"));//true
console.log(str1.startsWith("b"));//false
console.log(str1.endsWith("期"));//true
console.log(str1.endsWith("1"));//false
let str3 = "hello\nword"
console.log(str3); //换行输出
console.log(String.raw`hello\nword`);//不换行
var x = 1, y = 2, z = 3, m = 4
var arr1 = Array.of(x, y, z, m);
console.log(arr1);//输出数组
var liList = document.getElementsByTagName("li")
console.log(liList);//伪数组 HTMLCollection(3) [li, li, li]
liList=Array.from(liList)
console.log(liList);//真正数组 [li, li, li]
let arr2 = [1, 2, 3, 4, 5]
var num1 = arr2.find(function (value) {
return value > 4//布尔型
})
console.log(num1);//5
var index = arr2.findIndex(function (value) {
return value > 3;
})
console.log(index);//3
var newArr2 = arr2.fill(6,1,2);
console.log(newArr2);// [1, 6, 3, 4, 5]
console.log(arr2);// [1, 6, 3, 4, 5]
作用:对数组的键值对进行遍历,返回一个遍历器
通过for of进行遍历
for (let [index, value] of arr3.entries()) {
console.log(`我是第${index + 1}个元素,我的值是${value}`);
console.log(arr3);
}
for (let index of arr3.keys()) {
console.log(index);
}
for (let value of arr3.values()) {
console.log(value);
}
//有2个div,遍历,给每个div添加点击事件,输出值,
for (var i = 0; i < list.length; i++) {
list[i].onclick = function () {
console.log(i);//2 2
}
}
for (let i = 0; i < list.length; i++) {
list[i].onclick = function () {
console.log(i);//0 1
}
}
const list = [//数组内置对象
{ id: 3, name: "张三丰" },
{ id: 5, name: "张无忌" },
{ id: 13, name: "杨逍" },
{ id: 33, name: "殷天正" },
{ id: 12, name: "赵敏" },
{ id: 97, name: "周芷若" },
]
//所有姓杨的人
let arr_1 = list.filter(function (item, index) {
return item.name.startsWith("杨")
})
console.log(arr_1);
//所有包含天这个字的人
let arr_2 = list.filter(function (item, index) {
return item.name.includes("天")
})
console.log(arr_2);
//找到周芷若的id
let arr_4 = list.find(function (item, index) {
return item.name === "周芷若"
}).id
console.log(arr_4);