Js if else 优化

JavaScript个人感悟 if else 优化

优化

一般写法

大量的if else

 	/**
     * 判断是哪一种类型的元件
     * @param {*节点_name} e 
     * @param {*返回值} v ==> properties_config
     * @param {*当前元素} o 
     */
    function _chooseisname(e, v, o) {
        if (!e) return;
        /**
         * 文字
         */
        if (e._name == "文字") {
            return o.addProperties(v.text_properties)
        }
        /**
         * 负荷
         */
        if (e._name == "负荷") {
            // return (v.fh_properties, v.fhmr_properties)
            return (o.addProperties(v.fhmr_properties), o.addProperties(v.fh_properties))
        }
        /**
         * 发电机
         */
        if (e._name == "发电机") {
            return o.addProperties(v.fdj_properties)
        }
        /**
         * 母线
         */
        if (e instanceof ht.MuXian) {
            return o.addProperties(v.node_properties)
        }
        /**
         * 线路
         */
        if (e instanceof ht.Node) {
            return o.addProperties(v.edge_properties)
        }
        ....
    }

或者好一点的写 switch case

switch(e._name){
    case "文字":
       //语句
	 return o.addProperties(v.text_properties)
       break; //可选
    case "负荷" :
       //语句
      return (o.addProperties(v.fhmr_properties), o.addProperties(v.fh_properties))
       break; //可选
     ...
    //你可以有任意数量的case语句
    default : //可选
    ....
       //语句
}

优化写法

const active = [
        {
            name: "文字", data: (e, v, o) => o.addProperties(v.text_properties)
        },
        {
            name: "负荷", data: (e, v, o) => (o.addProperties(v.fhmr_properties), o.addProperties(v.fh_properties), o.addProperties(v.ys_properties))
        },
        {
            name: "发电机", data: (e, v, o) => (o.addProperties(v.fdj_properties), o.addProperties(v.ys_properties))
        }
    ]

   function _chooseisname(e, v, o) {
        if (!e) return;
        let newFundata = active.filter(item => item.name == e._name);
        if (newFundata.length == 0) {
            /**
             * 母线
             */
            if (e instanceof ht.MuXian) {
                return (o.addProperties(v.node_properties), o.addProperties(v.ys_properties))
            }
            /**
             * 线路
             */
            if (e instanceof ht.Node) {
                return (o.addProperties(v.edge_properties), o.addProperties(v.ys_properties))
            }
        } else {
            return newFundata[0].data(e, v, o)
        }
    }

或者

/**
     * 元件对应的默认值
     */
    const _Allmrz = [{
        name: "负荷",
        data: [
            ["MX_ID", "1"],
            ["KJQK", "1"],
            ["FH_YGGL", "1000"],
            ["FH_WGGL", "0"],
            ["HGL_YGGL", "1000"],
            ["HGL_WGGL", "0"],
            ["YYCL", "0"],
            ["DQB_LV", "15"],
            ["DJ_GLB", "15"],

            ["EDZCL", "0.0167"],
            ["JFS", "5"],
            ["Rs", "0.0035"],
            ["Xs", "0.1234"],
            ["Rr", "0.0133"],
            ["Xr", "0.1234"],
            ["xm", "2.0324"],
        ]
    },
    {
        name: "发电机",
        data: [
            ["MX_ID", "1"],
            ["MAX_YGGL", "10000"],
            ["MIN_YGGL", "0"],
            ["MAX_WGGL", "10000"],
            ["MIN_WGGL", "-10000"],
            ["ZT", "开"],
            ["GZB", "1"],
            ["XF_Time", "1"],
            ["YGXCXS", "0"],
            ["WGXCXS", "0"],
        ]
    },
    {
        name: "文字",
        data: [
            ["text.color", "#ffff"]
        ]
    }

    ]
    /**
     * 判断默认值的元件
     * @param {*当前所选的元素} s 
     */
    function _choosermr(s) {
        const isNull = [[]]
        if (!s) return;
        let newArry = _Allmrz.filter(item => item.name == s._name)
        return newArry ? newArry.length > 0 ? newArry[0].data : isNull : isNull
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值