s面向对象的写法

js面向对象的写法
一、在html中引入该js文件,使用时:

<script>
    var BuyBw8Product = new buyBw8Product();
</script>

二、一般写一个较大的模块的js代码时,采用这种方法写js

新建一个js,内容结构如下:

/**
 * create by wkk
 * 描述:***
 */
!function(){
    function b(){
        var that = this;
        //当前是否到期集合
        this.moduleIsExpired = {
            ticketIsExpired : $("#ticketIsExpired"),
            ccIsExpired     : $("#ccIsExpired"),
            imIsExpired     : $("#imIsExpired")
        };
        //工单购买坐席数的change事件
        $("#ticketSeatNum").on('change', (function(){
            var ticketSeatNum = $(this).val() ? $(this).val() : 0;
            if(parseInt(ticketSeatNum) <= 0){
                alert('请填写正确的工单购买坐席数');
                that.disableBuyButton();
                // 清空工单的金额,新计算余额
                that.emptyMoney('ticket');
                return false;
            }
            //购买工单周期
            var ticketSeatYear = parseInt($(that.SeatYearSelect.ticketSeatYear).val());
            //不存在周期,表示不过期并且原有坐席数不为0
            if($("#ticketSeatYear").length <= 0){
                //计算新的周期
                var ticketCurExpireTime = $(that.allCurExpireTime.ticketCurExpireTime).text();
                //从今天到到期日期的天数差值
                var todayDate = that.getTodayDate();
                var expireTimeDays = that.DateDiff(todayDate, ticketCurExpireTime);
                var ticketSeatYear = (parseInt(expireTimeDays)/365).toFixed(8);
            }
            if(ticketSeatYear > 0){
                //计算工单的金额
                var ticketPrice = $(that.productPrice.ticketPrice).text();
                //四舍五入->向下取整
                var ticketTotalAmount = Math.floor(that.calcAmount(ticketSeatNum, ticketPrice, ticketSeatYear));

                //显示模块金额和总金额以及折扣信息
                that.showModuleTotalAmountAndTotalMoneyAndDiscount('ticket', ticketTotalAmount, ticketSeatNum, ticketSeatYear);
            }
        }));
        /**
         * 比较两个日期的大小,返回值比较大的日期
         * @param date1
         * @param date2
         * @returns date
         */
        this.tab = function (date1,date2){
            var oDate1 = new Date(date1);
            var oDate2 = new Date(date2);
            if(oDate1.getTime() >= oDate2.getTime()){
                return date1;
            } else {
                return date2;
            }
        }

        /**
         * 返回今天的日期  格式:2018-08-08
         */
        this.getTodayDate = function(){
            var myDate = new Date;
            var year = myDate.getFullYear();//获取当前年
            var month = myDate.getMonth()+1;//获取当前月
            var date = myDate.getDate();//获取当前日
            return year+'-'+month+'-'+date;
        }

        /**
         * 计算天数差的函数,通用
         * @param sDate1
         * @param sDate2
         * @returns {Number}
         */
        this.DateDiff = function(sDate1,  sDate2){ //sDate1和sDate2是2006-12-18格式
            var  aDate,  oDate1,  oDate2,  iDays
            aDate  =  sDate1.split("-")
            oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式
            aDate  =  sDate2.split("-")
            oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])
            iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数
            return  iDays
        }
    }
    window.buyBw8Product = b;
}();
    原文作者:前端爱好者
    原文地址: https://segmentfault.com/a/1190000017063160
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞