首页 > 编程语言 >写好JavaScript条件语句的5条守则

写好JavaScript条件语句的5条守则

时间:2024-07-26 18:20:03浏览次数:8  
标签:语句 守则 console apple JavaScript fruit log red quantity

照抄https://juejin.im/post/5bdef288e51d450d810a89c6
testEquals(fruit) { if (fruit === 'apple' || fruit === 'strawberry') { console.log('=='); } }, testIncludes(fruit) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (redFruits.includes(fruit)) { console.log('red'); } }, testMoreIf(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; // 条件 1: fruit 必须有值 if (fruit) { // 条件 2: 必须是red的 if (redFruits.includes(fruit)) { console.log('red'); // 条件 3: quantity大于10 if (quantity > 10) { console.log('big quantity'); } } } else { console.log('error'); } }, testMoreNoElse(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (!fruit) console.log('error'); if (!redFruits.includes(fruit)) return; console.log('red'); if (quantity > 10) console.log('big quantity'); }, testOrValue(fruit, quantity) { if (!fruit) return; // 如果 quantity 参数没有传入,设置默认值为 1 const q = quantity || 1; console.log(`We have ${q} ${fruit}!`); }, testDefaultValue(fruit, quantity = 1) { // 如果 quantity 参数没有传入,设置默认值为 1 if (!fruit) return; console.log(`We have ${quantity} ${fruit}!`); }, testSwitch(color) { // 使用条件语句来寻找对应颜色的水果 switch (color) { case 'red': return ['apple', 'strawberry']; case 'yellow': return ['banana', 'pineapple']; case 'purple': return ['grape', 'plum']; default: return []; } }, testInsteadOfSwitch(color) { const fruitColor = { red: ['apple', 'strawberry'], yellow: ['banana', 'pineapple'], purple: ['grape', 'plum'] }; return fruitColor[color] || []; },
 //1.
      this.testEquals('apple'); //==
      this.testIncludes('apple'); //redz
      //2.
      this.testMoreIf(null); // error: No fruits
      this.testMoreIf('apple'); // print: red
      this.testMoreIf('apple', 20); // print: red, big quantity
      this.testMoreNoElse(null); // error: No fruits
      this.testMoreNoElse('apple'); // print: red
      this.testMoreNoElse('apple', 20); // print: red, big quantity
      //3.
      this.testOrValue('banana'); // We have 1 banana!
      this.testOrValue('apple', 2); // We have 2 apple!
      this.testDefaultValue('banana'); // We have 1 banana!
      this.testDefaultValue('apple', 2); // We have 2 apple!
      //4.
      testSwitch(null); // []
      testSwitch('yellow'); // ['banana', 'pineapple']
      testInsteadOfSwitch(null); // []
      testInsteadOfSwitch('yellow'); // ['banana', 'pineapple']

 

标签:语句,守则,console,apple,JavaScript,fruit,log,red,quantity
From: https://www.cnblogs.com/jishugaochao/p/11732381.html

相关文章

  • 用户管理与高级SQL语句(数据库管理与高可用)
    1.表(Table)数据库中的表与我们日常生活中使用的表格类似,它也是由行(Row)和列(Column)组成的。列由同类的信息组成,每列又称为一个字段,每列的标题称为字段名。行包括了若干列信息项。一行数据称为一个或一条记录,它表达有一定意义的信息组合。一个数据库表由一条或多条记录组成,没有......
  • DataFrame RHS语法查询语句
    RHS:https://github.com/acoboh/query-filter-jpa/blob/main/README.md规则:查询字段名:查询条件(条件值)关系符查询字段名:查询条件(条件值)...查询条件有:‒eq:等于‒gt:大于‒gte:大于等于‒lt:小于‒lte:小于等于‒ne:不等于‒cn:包含‒ncn:不包......
  • Python基础知识点(1)基本语句
    基本语句1.if语句if表达式:语句块其中,表达式是一个返回True或False的表达式。如果表达式为True,则执行if下面的语句块;如果为False,则跳过语句块执行下面的语句。2.if…else语句if表达式:语句块1else:语句块2其中,表达式是一个返回True或False的表达式。如果......
  • MySQL入门---(一)SQL的DDL语句
    1.管理员身份进入命令行窗口:win+rcmd然后不要直接点,按ctrl+shift+enter管理员模式进去,点确定2.MySQL数据库启动:netstartmysql80停止:netstopmysql803.系统自带的命令行工具执行指令:mysql-uroot-p1.SQL通用语法:2.DDL语句3.表结构查询:4.创建表结构5.表操作--......
  • 006-绕过web检查,传输sql语句的功能
    importorg.apache.commons.text.StringEscapeUtils;/***作用:*实现绕过web检查,传输sql语句的功能**pom:*org.apache.commons:commons-lang3:3.12.0*org.apache.commons:commons-text:1.10.0*/publicclassMain{publicstaticvoidmain(String[]arg......
  • Day4-控制语句
    1.分支语句1.1顺序语句,分支语句,循环语句分支语句if()~else~switch循环语句for()while()~do~while()goto辅助控制语句continuebreakreturn1.2if~else语句的使用if语句概述if(表达式)语句块1else......
  • 基本的DQL语句-单表查询
    一、DQL语言        DQL(DataQueryLanguage数据查询语言)。用途是查询数据库数据,如SELECT语句。是SQL语句中最核心、最重要的语句,也是使用频率最高的语句。其中,可以根据表的结构和关系分为单表查询和多表联查。二、单表查询        针对数据库中的一张......
  • 跳转语句
     一、break语句在for循环中使用break:publicclassBreakExample{ publicstaticvoidmain(String[]args){   for(inti=0;i<10;i++){     if(i==5){       break;     }     System......
  • Web应用课 3.3 JavaScript——对象、数组、函数、类、事件
    对象英文名称:Object。类似于C++中的map,由key:value对构成。value可以是变量、数组、对象、函数等。函数定义中的this用来引用该函数的“拥有者”。eg.letperson={name:'zjq',age:18,money:100,friends:['yxc','Bob','Lucy'],//对象成员可以是数......
  • JavaScript操作BOM与DOM
    BOM操作window对象window对象是整个BOM的核心,表示浏览器打开的窗口。属性属性                              描述history     用户访问过的URL信息(历史记录)location        当前URL的信息。screen   ......