前端实现购物车js代码

    function Goods(goodsId, goodsNum, goodsStatus) {
        // 0.正常可下单    1.缺货  2.不存在
        this.goodsId = goodsId;
        Goods.prototype.goodsNum = 0;
        if( typeof goodsNum ==="undefined")
        {
            this.goodsNum = Goods.prototype.goodsNum;
        } else {
            this.goodsNum=goodsNum;
        }
        if( typeof goodsStatus ==="undefined")
        {
            this.goodsStatus = Goods.prototype.goodsNum;
        } else {
            this.goodsStatus=goodsStatus;
        }
        Goods.prototype.setGoodsNum = function (goodsId) {
            this.goodsId = goodsId;
        };
        Goods.prototype.setGoodsStatus = function (goodsStatus) {
            this.goodsStatus = goodsStatus;
        };
    }

    var GoodsList = {
        goodsList: [],
        addGoods: function (goods) {
            for (var i = 0; i < this.goodsList.length; i++) {
                if (this.goodsList[i].goodsId === goods.goodsId) {
                    this.goodsList[i].goodsNum+=goods.goodsNum;
                    return;
                }
            }
            this.goodsList.push(goods);
        },
        setGoods: function (goods) {
            for (var i = 0; i < this.goodsList.length; i++) {
                if (this.goodsList[i].goodsId === goods.goodsId) {
                    this.goodsList[i].goodsNum=goods.goodsNum;
                    this.goodsList[i].goodsStatus=goods.goodsStatus;
                    return;
                }
            }
            this.goodsList.push(goods);
        },
        getGoods: function (goodsId) {
            for (var i = 0; i < this.goodsList.length; i++) {
                if (this.goodsList[i].goodsId === goodsId) {
                    return this.goodsList[i];
                }
            }
            return new Goods(goodsId);
        },
        removeGoods:function (goodsId) {
            for (var i = 0; i < this.goodsList.length; i++) {
                if (this.goodsList[i].goodsId === goodsId) {
                    this.goodsList.splice(i,1);
                    return;
                }
            }
        },
        getGoodsCookie:function () {
            return JSON.stringify(this.goodsList);
        },
        setStringCookie:function (cookieList) {
            if (cookieList !== null && cookieList !== undefined && cookieList !== ''&&cookieList!==""&&cookieList.trim().length>0) {
                this.goodsList = JSON.parse(cookieList);
                return;
            }
        }
    };

    var Cookie={
        getShoppingCookie:function () {
            return (function (name)
            {
                var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
                if(arr=document.cookie.match(reg)){
                    var s = decodeURI(arr[2]);
                    if (s[0]=='\"'){
                        return "";
                    }else {
                        return decodeURI(arr[2]);
                    }
                }
                else
                    return "";
            })("shoppingCar");
        },
        setShoppingCookie:function (shoppingCar) {
            document.cookie="shoppingCar="+shoppingCar+"; path=/";
        }
    };

你可能感兴趣的:(JAVA)