亲宝软件园·资讯

展开

js if语句优化

程序员布欧 人气:0

前言

if else 基本使用

let name = "zhangsan";
if (name === "zhangsan") {
	console.log("zhangsan"); // zhangsan
} else {
	console.log("lisi");
}

简化if判断和优化代码

单行if else

if (name === "zhangsan") console.log("zhangsan");
else console.log("lisi");

使用&& || 优化

let userInfo = {
	name: "zhangsan",
};
if (userInfo.name === "zhangsan") {
	console.log("zhangsan");
}
if (!userInfo.age) {
	// userInfo.age--> false age属性不存在
	console.log("年龄属性不存在");
}
userInfo.name === "zhangsan" && console.log("zhangsan");
userInfo.age || console.log("年龄属性不存在"); // 年龄属性不存在

使用三目运算符优化

let name = "zhangsan";
name === "zhangsan" ? console.log("zhangsan") : console.log("lisi");

合并if

let userObj = { name: "zhangsan", children: { name: "lisi" } };
if (userObj.children) {
  if (userObj.children.name == "lisi") {
    console.log(userObj.children.name);
  }
}
if (userObj.children && userObj.children.name == "lisi")
  console.log(userObj.children.name);

使用includes 或者indexof

let age = "22";
if (age == "22" || age == "24" || age == "25") {
  console.log(age);
}
let ageArray = ["22", "24", "25"];
if (ageArray.includes(age)) {
  console.log(age);
}
if (ageArray.indexOf(age) > -1) {
  console.log(age);
}

使用switch流程 优化 多个if else

let val = "lisi";
switch (val) {
  case "zhangsan":
    console.log(val);
    break;
  case "lisi":
    console.log(val);
    break;
  case "xiaoming":
    console.log(val);
    break;
  case "xiaowang":
    console.log(val);
    break;
  default:
    console.log(val);
}

使用对象 key-value 优化多条if语句

let userArray = [
  { name: "zhangsan" },
  { name: "lisi" },
  { name: "xiaoming" },
  { name: "xiaowang" },
];

// 多重if else
if (userArray.name === "zhangsan") {
  console.log(userArray.name);
} else if (userArray.name === "lisi") {
  console.log(userArray.name);
} else if (userArray.name === "xiaoming") {
  console.log(userArray.name);
} else if (userArray.name === "xiaowang") {
  console.log(userArray.name);
} else {
  console.log("其他用户");
}

function getUser(username) {
  return userArray.filter((item) => item.name == username)[0];
}
console.log(getUser("zhangsan"));

使用map

let filterUser = function (username) {
  return `名字是${username}`;
};
let userMap = new Map([
  ["zhangsan", filterUser("zhangsan")],
  ["lisi", filterUser("lisi")],
  ["xiaoming", filterUser("xiaoming")],
  ["xiaowang", filterUser("xiaowang")],
]);
console.log(userMap.get("zhangsan"));   // 名字是zhangsan

推荐一些常用的JavaScript语法糖

箭头函数

/**
 * 箭头函数
 *
 * */
function userInfo(name, age) {
  return `名字是:${name},年龄是:${age}`;
}
let userInfo = (name, age) => `名字是:${name},年龄是:${age}`;
console.log(userInfo("zhangsan", "23"));

三目运算符处理函数

function log1() {
  console.log(1);
}
function log2() {
  console.log(2);
}
let num = 2;
if (num === 1) {
  log1();
} else {
  log2();
}
num === 1 ? log1() : log2();

函数默认参数处理

let getUser = (name = "zhangsan", age = 23) =>
      `名字是:${name},年龄是:${age}`;

console.log("默认参数", getUser());
console.log("传参", getUser("lisi", 29));

数据类型转换

let num1 = +"123";
console.log(num1);

Null、Undefined 布尔值等特殊值处理,使用||结合!

/**
 *
 * Null、Undefined 布尔值等特殊值处理,使用||结合!
 *
 * */
let name1 = undefined;
let name2 = null;
let name3 = true;
let name4 = false;
let name5 = "lisi";
console.log(name1 || "其他人"); // 其他人
console.log(name2 || "其他人"); // 其他人
console.log(name3 || "其他人"); // true
console.log(!name3 || "其他人"); // 其他人
console.log(name4 || "其他人"); // 其他人
console.log(name5 || "其他人"); // lisi
console.log(!name5 || "其他人"); // 其他人

链判断运算符

?.运算符在运算过程中,会首先从左到右执行,左侧的对象是否为null或undefined。如果是的,就不再往右边执行运算,而是返回undefined

更多链判断运算符请参考

链判断运算符

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Optional_chaining

let user = {
  name: "zhangsan",
  children: { name: "lisi", children: { name: "xiaoming" } },
};
let name1 = user?.name;
let name2 = user?.children?.name;
let name3 = user?.children?.children?.name;
console.log(name1);
console.log(name2);
console.log(name3);

空值合并操作符 ??

概念

const foo = null ?? "default string";
const test = undefined ?? "default string";
const test1 = 123 ?? "default string";
console.log(test); // "default string"
console.log(foo); // "default string"
console.log(test1); // 123
const baz = 0 ?? 42;
console.log(baz); // 0
const str = "" ?? 42;
const str1 = "" || 42;
console.log(str); // ""
console.log(str1); // 42

小结

源码地址

码云 https://gitee.com/lewyon/vue-note

githup https://github.com/akari16/vue-note

加载全部内容

相关教程
猜你喜欢
用户评论