Fork me on GitHub

关于return的使用问题


  废话不多少,看代码体会

不使用return

        add() {
          if (this.brandName === '') {
            alert('未填写品牌名称')
          } else {

            // 创建模拟数据
            const obj = {
              id: 4,
              brand: this.brandName,
              status: false,
              buildTime: new Date()
            }
            // 添加数据
            this.list.push(obj)

            // 收尾处理
            this.brand = ""
            this.nextid++
          }
        }

使用return

        // 即阻止默认提交行为后,触发add方法
        add() {
          if (this.brandName === '') {
            return alert('未填写品牌名称')
          }

          // 创建模拟数据
          const obj = {
            id: 4,
            brand: this.brandName,
            status: false,
            buildTime: new Date()
          }
          // 添加数据
          this.list.push(obj)

          // 收尾处理
          this.brand = ""
          this.nextid++
        }

总结

  经过对比发现:使用return后,后续代码不会有太多的约束感。推荐使用return🤔。

完整的Demo代码:
  地址:

posted @   Lencamo  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示