基础类型:

<script>
        let name = "zhangsan"
        let body={
            name:"zhangsan",
            age:15,
            isboy:true
        }
        // map
        let girl = new Map([
            ["name","lxy"],
            ["age",24]
        ])
        // set
        let num = new Set([1,2,3,2])  //不重复
        //arr
        let str = ["a","b","c"]
        // 函数
        function add(x,y){
            return x+y
        }
        // 匿名函数
        let sum = function(x,y){
            return x+y
        }
        // 箭头函数
        let plus=(x,y)=>{
            return x+y
        }
        //类
        class Person{
            name
            age
            constructor(name,age){
                this.name= name
                this.age = age
            }

            info(){
                console.log("姓名:" +this.name + "年龄:"+ this.age)
            }
        }
        console.log(name)
        console.log(body)
        console.log(girl)
        console.log(num)
        console.log(str)
        console.log(add(20,30))
        console.log(sum(20,30))
        console.log(plus(20,30))
        const person= new Person("lxy",24)   //实例化
        person.info()
    </script>

数组的操作:

<script>
    let arr =[10,11]
    console.log(arr);
    // 末尾添加元素
    let arrLen=arr.push(12,14)
    console.log(arr);
    console.log(arrLen);
    // 开头添加
    arrLen= arr.unshift(8,9)
    console.log(arr);
    console.log(arrLen);
    // 开头删除
    arrLen= arr.shift()
    console.log(arr);
    console.log(arrLen);
    // 尾部删除
    arrLen= arr.pop()
    console.log(arr);
    // 删除n位置的n个元素
    arrLen= arr.splice(2,2)
    console.log(arr);
    // 颠倒
    arrLen= arr.reverse()
    console.log(arr);
    // 按照首字母排序
    let arr2=['banana','apple','orange']
    arr2.sort()
    console.log(arr2);
    // 按照大小排序
    let arr3=[5,6,1,4,7,3]
    arr3.sort((a,b)=>a-b)
    console.log(arr3);
    // 条件过滤
    let newArr=arr3.filter((value,index)=>{
        return value>4
    })
    console.log(newArr);
    //合并数组
    let arr4 = ['aaa','bbb','ccc']
    newArr= arr3.concat(arr4,"ddd")
    console.log(newArr);
    // 遍历数组
    for(let val of newArr){
        console.log(val);
    }
    newArr.forEach((val,index)=>{
        console.log(index+":="+val);
    })
</script>
最后修改:2024 年 07 月 04 日
如果觉得我的文章对你有用,请随意赞赏