JavaScript简写优化方法

1、如果有多个条件

我们可以在数组中存储多个值,并且可以使用数组 include 方法。

//优化前
if (
  x === "abc" ||
  x === "def" ||
  x === "ghi" ||
  x === "jkl"
) {
  //logic
}

//优化后
//Shorthand
if (["abc", "def", "ghi", "jkl"].includes(x)) {
  //logic
}

2、如果只有一个条件

这对于我们有 if-else 条件,里面不包含更大的逻辑时,是一个较大的捷径。我们可以简单的使用三元运算符来实现这个简写。

// 优化前
let test: boolean;
if (x > 100) {
  test = true;
} else {
  test = false;
}

// 优化后
let test = x > 10 ? true : false;
//or we can use directly
let test = x > 10;

3.声明变量

当我们要声明两个具有共同值或共同类型的变量时,可以使用此简写形式。

//优化前
let test1;
let test2 = 1;

//优化后
let test1,
  test2 = 1;

4、null/undefined 值检查和分配默认值

let test1 = null,
  test2 = test1 || "";

console.log("null check", test2); // output will be ""

let test1 = undefined,
  test2 = test1 || "";

console.log("undefined check", test2); // output will be ""

//正常值检查

let test1 = "test",
  test2 = test1 || "";

console.log(test2); // output: 'test'

5.将值分配给多个变量

当我们处理多个变量并希望将不同的值分配给不同的变量时,可以用解构赋值此简写技术非常有用。

//优化前
let test1, test2, test3;
test1 = 1;
test2 = 2;
test3 = 3;

//优化后
let [test1, test2, test3] = [1, 2, 3];

6.赋值运算符简写

我们在编程中处理很多算术运算符,这是将运算符分配给 JavaScript 变量的有用技术之一。

// Longhand
test1 = test1 + 1;
test2 = test2 - 1;
test3 = test3 * 20;

// Shorthand
test1++;
test2--;
test3 *= 20;

7.用for...in与for...of

// Longhand
for (var i = 0; i < testData.length; i++)

// Shorthand
for (let i in testData) or  for (let i of testData)

8.箭头函数

//Longhand
function add(a, b) {
  return a + b;
}

//Shorthand
const add = (a, b) => a + b;

9.短函数调用

我们可以使用三元运算符来实现这些功能

// Longhand
function test1() {
  console.log("test1");
}
function test2() {
  console.log("test2");
}
var test3 = 1;
if (test3 == 1) {
  test1();
} else {
  test2();
}

// Shorthand
(test3 === 1 ? test1 : test2)();

10.Switch 简写

// Longhand
switch (data) {
  case 1:
    test1();
    break;

  case 2:
    test2();
    break;

  case 3:
    test();
    break;
  // And so on...
}

// Shorthand
var data = {
  1: test1,
  2: test2,
  3: test,
};

data[something] && data[something]();

11.用解构简写

//longhand
const test1 = this.data.test1;
const test2 = this.data.test2;
const test2 = this.data.test3;

//shorthand
const { test1, test2, test3 } = this.data;

12.Array.find

当我们确实有一个对象数组并且我们想要根据对象属性查找特定对象时,find 方法确实很有用。

const data = [
  {
    type: "test1",
    name: "abc",
  },
  {
    type: "test2",
    name: "cde",
  },
  {
    type: "test1",
    name: "fgh",
  },
];
function findtest1(name) {
  for (let i = 0; i < data.length; ++i) {
    if (data[i].type === "test1" && data[i].name === name) {
      return data[i];
    }
  }
}

//Shorthand
filteredData = data.find(
  (data) => data.type === "test1" && data.name === "fgh"
);
console.log(filteredData); // { type: 'test1', name: 'fgh' }

13.查找条件简写

如果我们有代码来检查类型,根据类型需要调用不同的方法,我们可以选择使用多个 else ifs 或者 switch,但是如果我们有比这更好的简写方法呢?

// Longhand
if (type === "test1") {
  test1();
} else if (type === "test2") {
  test2();
} else if (type === "test3") {
  test3();
} else if (type === "test4") {
  test4();
} else {
  throw new Error("Invalid value " + type);
}

// Shorthand
var types = {
  test1: test1,
  test2: test2,
  test3: test3,
  test4: test4,
};

var func = types[type];
!func && throw new Error("Invalid value " + type);
func();
原文地址:https://www.cnblogs.com/yixiancheng/p/15147624.html