• js编程思路把相同功能的代码,配置放到一个对象里


    以京东代码为例:

    对应代码:

    从图中可以看出增加减函数都写在了一个对象setAmount中

    var setAmount = {
        min: 1,
        max: 999,
        urlPerfix: "http://gate.360buy.com/InitCart.aspx?",
        targetLink: $("#choose-btn-append .btn-append"),
        data: {
            pid: G.sku,
            pcount: $("#buy-num").val(),
            ptype: 1
        },
        reg: function(a) {
            return new RegExp("^[1-9]\\d*$").test(a)
        },
        amount: function(b, c) {
            var a = $(b).val();
            if (this.reg(a)) {
                if (c) {
                    a++
                } else {
                    a--
                }
            } else {
                alert("\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u6570\u91cf\uff01");
                $(b).val(1);
                $(b).focus()
            }
            return a
        },
        reduce: function(b) {
            var a = this.amount(b, false);
            if (a >= this.min) {
                $(b).val(a)
            } else {
                alert("\u5546\u54c1\u6570\u91cf\u6700\u5c11\u4e3a" + this.min);
                $(b).val(1);
                $(b).focus()
            }
            if (this.data.pcount) {
                this.data.pcount = $("#buy-num").val()
            }
            this.updateBuyLink(G.sku, this.urlPerfix)
        },
        add: function(b) {
            var a = this.amount(b, true);
            if (a <= this.max) {
                $(b).val(a)
            } else {
                alert("\u5546\u54c1\u6570\u91cf\u6700\u591a\u4e3a" + this.max);
                $(b).val(999);
                $(b).focus()
            }
            if (this.data.pcount) {
                this.data.pcount = $("#buy-num").val()
            }
            this.updateBuyLink(G.sku, this.urlPerfix)
        },
        modify: function(b) {
            var a = $(b).val();
            if (a < this.min || a > this.max || !this.reg(a)) {
                alert("\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u6570\u91cf\uff01");
                $(b).val(1);
                $(b).focus()
            }
            if (this.data.pcount) {
                this.data.pcount = $("#buy-num").val()
            }
            this.updateBuyLink(G.sku, this.urlPerfix)
        },
        updateBuyLink: function(f, a) {
            var c = this.data.pcount;
            var b = /^[0-9]{1,4}$/;
            var d = a || "http://gate.360buy.com/InitCart.aspx?";
            if (!b.exec(c)) {
                c = 1
            } else {
                if (c <= 0) {
                    c = 1
                }
                if (c >= 1000) {
                    c = 999
                }
            }
            if (typeof RecoServices.cacheData.ybId !== "undefined") {
                this.targetLink.attr("href", d + $.param(this.data) + "&ybId=" + RecoServices.cacheData.ybId)
            } else {
                this.targetLink.attr("href", d + $.param(this.data))
            }
        }
    };

    从代码中可以看出,相同的代码和配置参数都写在一个对象里,这样好管理

  • 相关阅读:
    什么是PostBack(译)
    GridView 多余字符显示省略号,并在Tooltip中显示完整信息
    GridView中添加行单击事件.md
    ASP.NET WebForm 通过 PagedDataSource 实现 Repeater 的分页
    回发或回调参数无效(译)
    jQuery UI Autocomplete Combobox 配 ASP.NET DropDownList
    6个重要的.NET概念:
    ASP.NET 使用Session,避免用户F5刷新时重复提交(转)
    支持行单击、双击事件的GridView和DataList控件(译)
    在TextBox中敲击回车执行ASP.NET后台事件
  • 原文地址:https://www.cnblogs.com/Alight/p/2890832.html
Copyright © 2020-2023  润新知