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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞