zoukankan      html  css  js  c++  java
  • js的一些小技巧

    使用 !! 在 JS 中将任何内容转换为布尔值

    !!true; //true
    !!false; //false
    !!0; // false
    !![]; //false
    !!''; //false
    !!'123'; //true

    使用扩展运算符组合两个数组

    const nums1 = [1, 2, 3];
    const nums2 = [4, 5, 6];
    const nums3 = [...nums1,...nums2];//[1,2,3,4,5,6]
    const nums1 = [1,2,3];
    const nums2 = [...nums1,4,5];//[1,2,3,4,5]

    使用扩展运算符将剩余元素分配给变量:

    const student = {
      name: "Matt",
      age: 23,
      city: "Helsinki",
      state: "Finland",
    };
    
    const { name, age, ...address } = student;

    使用&&运算符检查是否为真。

    对象键与值具有相同的名称

    const name = "Luis", city = "Paris", age = 43, favoriteFood = "Spaghetti";
    
    const person = { name, city, age, favoriteFood };

    为函数参数提供默认值

    function pickUp(fruit = "Banana") {
      console.log(`I picked up a ${fruit}`)
    }
    
    
    pickUp("Mango"); // -> I picked up a Mango
    pickUp();        // -> I picked up a Banana

    用于Object.values()将对象的所有值收集到一个新数组中

    const info = { name: "Matt", country: "Finland", age: 35 };
    
    const data = Object.values(info);

    includes和indexof检查元素是否在数组中

    let numbers = [1, 2, 3];
    
    const hasNumber1 = numbers.indexOf(1) > -1 // -> True
    
    const hasNumber1 = numbers.includes(1)     // -> True

    压缩多个条件

    const num = 1;
    
    if([1,2,3].includes(num)){
      console.log("Yay");
    }

    **运算符替代Math.pow()

    Math.pow(4,2); // 16
    Math.pow(2,3); // 8
    
    
    4**2 // 16
    2**3 // 8

    ~~运算符替代Math.floor()

    Math.floor(5.25) // -> 5.0
    
    ~~5.25 // -> 5.0

    使用解构语法在一行中分配多个值:

    let num1, num2;
    
    [num1, num2] = [10, 100];
    student = {
      name: "Matt",
      age: 29,
    };
    
    let { name, age } = student;
  • 相关阅读:
    QString 与 string转换
    Lua 判断表是否为空方法
    QT 继承QWidget && 继承QDialog
    QNetworkAccessManager
    Lua语言特色
    Lua逻辑操作符
    Lua语言总结
    Lua 函数
    Lua数据类型
    QT 通过QNetworkReply *获取对应请求的URL地址
  • 原文地址:https://www.cnblogs.com/houBlogs/p/15094640.html
Copyright © 2011-2022 走看看