美文网首页
继承实例

继承实例

作者: 快乐小码仔 | 来源:发表于2020-04-29 15:54 被阅读0次

    继承实例

    //定义产品对象
    function Base(){
        /*产品名称*/
        this.name=''
        this.description=''
        /*普通价格*/
        this.normalPrice=144
        /*团购价格*/
        this.youhuijia=120
        /*已经购买的人数*/
        this.buySum=100;
        /*轮播图片列表*/
        this.images=[]
    }
    Base.prototype={
        init:function(){},
        /*普通购买*/
        buy:function(){},
        /*绑定图片列表*/
        bindDOMImage:function(){
            var str=''
            for(var i= 0,len=this.images.length;i<len;i++) {
                str+='<li>'
                str+='<img class="etalage_thumb_image" src="'+ this.images[i].small+'" class="img-responsive" />'
                str+='<img class="etalage_source_image" src="'+ this.images[i].small+'" class="img-responsive" />'
                str+='</li>'
            }
            $('#etalage').html(str)
    
            /*jquery插件实现的幻灯片特效*/
            $('#etalage').etalage({
                thumb_image_width: 250,
                thumb_image_height: 300,
            });
        },
        /*绑定详细信息*/
        bindDOMDetail:function(){},
        /*绑定事件*/
        bindEvents:function(){},
        /*团购*/
        groupBuy:function(){},
    }
    
    /*衣服*/
    var Close = function(){
        Base.call(this, arguments);
        this.sizes=['X','XL','XXL']
        this.colors=['黄色','红色']
    }
    /*原型写法*/
    Close.prototype = new Base();
    Close.prototype.bindDOMDetail= function(){}
    Close.prototype.bindSizes = function(){
    }
    Close.prototype.bindColors = function(){
        var str= ''
        str+= '<h3>颜色</h3>'
        for(var i=0;i<this.colors.length;i++){
            str+='<li><a href="#">'+this.colors[i]+'</a></li>'
        }
        $('.colors').html(str)
    }
    Close.prototype.init= function(){
        this.bindDOMDetail()
        this.bindDOMImage()
        this.bindSizes()
        this.bindColors()
    }
    
    
    /*书籍*/
    var Book = function(){
        Base.call(this, arguments);
        this.author='糖葫芦'
        this.publisher = '清华大学出版社'
        this.pages = 333
        this.publishTimes = 2
        this.type='IT教育'
        this.publishTime='2016-09-09'
    
    }
    /*原型写法*/
    Book.prototype = new Base();
    /*重写 覆盖基类方法*/
    Book.prototype.bindDOMDetail= function(){}
    Book.prototype.readTry= function(){}
    Book.prototype.readAll= function(){}
    Book.prototype.init= function(){
        this.bindDOMDetail()
        this.bindDOMImage()
    }
    /*不能使用如下写法*/
    //Book.prototype = {}
    /*为什么
        因为这个写法相当于重新定义一个原型对象*/
    

    相关文章

      网友评论

          本文标题:继承实例

          本文链接:https://www.haomeiwen.com/subject/foqnwhtx.html