Vue的三个点es6知识,扩展运算符

Vue中的三个点在不同情境下的意思

  • 操作数组
    //里面放自己定义的方法
    methods: {
      /**
       * 把数组中的元素孤立起来
       */
      iClick() {
        let iArray = ['1', '2', '3'];
        console.log(...iArray);
        // 打印结果  1 2 3
      },

      /**
       * 在数组中添加元素
       */
      iClick3() {
        let iArray = ['1', '2', '3'];
        console.log(['0', ...iArray, '4']);
        // 打印结果  ["0", "1", "2", "3", "4"]
      },

      /**
       * 在数组中删除元素(取出一个元素)
       * 与结构赋值的结合
       * 如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。
       */
      iClick8() {
        const [first, ...rest] = [1, 2, 3, 4, 5];
        console.log(first);
        // 打印结果 1
        console.log([...rest]);
        // 打印结果 [2, 3, 4, 5]

        const [one, ...last] = ["foo"];
        console.log(one);
        //打印结果 foo
        console.log([...last]);
        //打印结果 []
      },

      /**
       * 数组的合并
       */
      iClick6() {
        // ES6 的写法
        var arr1 = [0, 1, 2];
        var arr2 = [3, 4, 5];
        arr1.push(...arr2);
        console.log(arr1);
        //  打印结果 [0, 1, 2, 3, 4, 5]
      },

      /**
       * 数组的合并(推荐使用)
       */
      iClick7() {
        var arr1 = [0, 1, 2];
        var arr2 = [3, 4, 5];
        console.log([...arr1, ...arr2]);
        //  打印结果 [0, 1, 2, 3, 4, 5]
      },

      /**
       * 将字符串转成数组
       */
      iClick9() {
        let iString = 'woshizhongguoren';
        console.log([...iString]);
        //  打印结果 ["w", "o", "s", "h", "i", "z", "h", "o", "n", "g", "g", "u", "o", "r", "e", "n"]
      },

      /**
       * Map 和 Set 结构, Generator 函数
       */
      iClick10() {
        let map = new Map([
          [1, 'one'],
          [2, 'two'],
          [3, 'three'],
        ]);
        let arr = [...map.keys()];
        console.log(arr);
        //  打印结果 [1, 2, 3]

      },

      /**
       * 当做参数传递
       * 和直接传数组的区别
       */
      iClick4() {
        let iArray = ['1', '2', '3'];
        //注意传的时候,就要三个点
        this.hanshu(...iArray);
      },
      hanshu(...iArray) {
        let ooo = 1;
        console.log(...iArray);
        //  打印结果 1 2 3
      },

      /**
       * 求出最大值
       */
      iClick5() {
        let iArray = [1, 2, 3, 99, 44, 66, 21, 85, 77];
        let ooo = Math.max(...iArray);
        console.log(ooo);
        //  打印结果 99
      },

      /**
       * 如果对没有iterator接口的对象,使用扩展运算符,将会报错。
       */
      iClick11() {
        let obj = {
          name: 'zhh',
          age: '20'
        }
        console.log([...obj]);
      },

    }

  • 操作对象
 methods: {

      /**
       * 添加一个属性
       */
      method3() {
        let a = {age: 18, id: 10};
        // 把 name 属性,放到对象中
        let c = {name: 'zhh', ...a};
        console.log(c);
        //  打印结果  {name: "zhh", age: 18, id: 10}

      },

      /**
       * 修改一个属性
       */
      method2() {
        let a = {name: 'zhh', age: 18, id: 10};
        //先拿到a, 后面的name:zhh1,把 a 中name 的值替换掉了
        let c = {...a, name: 'zhh1'};
        console.log(c);
        // 打印结果  {name: "zhh1", age: 18, id: 10}

      },

      /**
       * 删除一个属性(拿出属性或者对象)
       */
      method1() {
        let a = {name: 'zhh', age: 18, id: 10};
        let {name, ...c} = a;
        console.log(name, c);
        //  打印结果 zhh {age: 18, id: 10}
      },

    }

原文地址:https://www.cnblogs.com/lz0925/p/11731120.html

时间: 2024-08-01 15:59:26

Vue的三个点es6知识,扩展运算符的相关文章

ES6数组扩展运算符和字符串遍历的新方法!!!

该方法主要是让我们在学习和工作中能够更加方便的去操作数据,数组和字符串! 下面开始代码展示阶段: 1.扩展运算符 console.log(...[1,2,3]) //以上结果输出:1,2,3 console.log(1,...[2,3,4],5) //以上结果输出:1,2,3,4,5 可以看出我在数组的前面使用了三个点,这个方法就是ES6的扩展运算符,它能够把数组给解析出来,让数组变成数值 更方便的拿到数据! console.log([...[],1]) //以上输出:[1] 但是扩展运算符后面

Js es6中扩展运算符(...)

拓展运算符,是es6一个很好的特性,它们可以通过减少赋值语句的使用,或者减少通过下标访问数组或对象的方式,使代码更加简洁优雅,可读性更佳.下面我将列出拓展运算符的主要应用场景,以及相关知识. 1.在函数调用时使用拓展运算符. 以前如果我们想将数组元素迭代为函数参数使用,一般使用Function.prototype.apply的方式. function myFunction(x, y, z) { console.log(x+""+y+""+z); } var args

ES6的扩展运算符和rest参数

1.扩展运算符(spread)是三个点(...).它好比rest参数的逆运算,将一个数组转为用逗号分隔的参数序列. 比如 var list=['大家好']: list.length=1.list[0]=大家好: var list2=[...'大家好']: list2.length = 3:list2[0]=大:list2[1]=家:list2[2]=好: 扩展运算符最有用之一是:Math.max(...arr);比较数组中的最大值. 2.rest参数 rest参数(形式为"...变量名"

Python学习之路(三):基础知识之运算符

1.while循环 1.1 while基本格式 while循环体格式为: while 循环条件: 循环体1 循环体2 1 count = 1 2 while count <= 10: # 循环条件的关键在于控制循环次数 3 if count != 7: 4 print(count) 5 count = count + 1 1.2 break 运用break能够在适当的条件下终止循环,即跳出当前循环,执行循环外下面的语句. 1 num = 1 2 while num < 3: 3 print(n

ES6扩展运算符的用途

ES6的扩展运算符可以说是非常使用的,在给多参数函数传参,替代Apply,合并数组,和解构配合进行赋值方面提供了很好的便利性. 扩展运算符就是三个点“...”,就是将实现了Iterator 接口的对象中的每个元素都一个个的迭代并取出来变成单独的被使用. 看这个例子: console.log(...[3, 4, 5]) 结果: 3 4 5 调用其实就是: console.log(3, 4, 5) 合并数组 可以使用扩展运算符将多个数组进行合并. let arr1 = [1, 2, 3] let a

ES6 - 数组扩展(扩展运算符)

扩展运算符 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算(函数),将一个数组转为用逗号分隔的参数序列. rest: 变量将多余的参数放入数组中. spread(扩展):rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. 总结:二者为逆操作. console.log(...[1, 2, 3]) // 1 2 3 该运算符主要用于函数调用. /** * 1.该运算符将一个数组,变为参数序列. */ function add(x, ...y) { // x =

数组的扩展运算符

一.基本使用 ES6中函数可以使用 rest参数 接收函数的多余参数,组成一个数组,放在形参的最后面. let fn = (a, ...value) => { console.log(a); console.log(value[0], value[1]); }; add(10, 20, 30); // 10 // 20 30 数组中的扩展运算符就好比 rest参数 的逆运算,将一个数组转为用逗号分隔的参数序列(也就是展开数组),在语法上,用三个点表示(...). var fruits = ['a

es6 扩展运算符 三个点(...)

1  含义 扩展运算符( spread )是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // 1 2 3 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5 [...document.querySelectorAll('div')] // [<div>, <div>, <div>] 该运算符主要用于函数调用. function push(

ES6扩展运算符(三点运算符)“...”用法和对象拷贝

es6拷贝数组对象有以下方法: 方法一: Object.assign() // 对象浅拷贝,obj1复制给obj2,这种方法要把obj2设置为{},不能const obj2 = ""; const obj1 = {a: 1}; const obj2 = {}; Object.assign( obj2, obj1) 方法二 :ES6扩展运算符(...) //对象浅拷贝,obj1复制给obj2 const obj1 = {a: 1}; const obj2 = {...obj1}; 方法三