• javascript原型,继承


    //普通对象
    //函数对象(有原型 prototy 的属性)
    //原型的应用 继承
    function Amial(){
        this.type = '小于'
    }
    function cat(name){
        this.name = name
    }
    cat.prototype = new Amial()
    var cat1 = new cat('小张')
    console.log(cat1.name,cat1.type)
    
    //构造器  继承 缺点:把父元素的属性都复制了
    function Amial(){
        this.type = '动物'
    }
    function amm(name){
        Amial.apply(this) //调用Amial构造器 Amial == amm
        //Amial.call(this,x,y,z)  Amial.apply(this,[x,y,z])
        this.name = name
    }
    var a = new amm('张三')
    console.log(a.type)
    
    //组合继承(原型+构造器) 缺点:调用两次父类构造器
    
    function Preson(name){
        this.arr = ['js','php']
        this.name = name
    }
    Preson.prototype.showname = function(){
        console.log(this.name)
    }
    function teacher(name,gread){
        Preson.call(this,name)
        this.gread = gread
    }
    teacher.prototype = new Preson() // 最关键的一句
    var a = new teacher('xiaoming', 23)
    console.log(a.arr,a.name,a.gread)
    a.showname()
    
    //原型+构造+寄生
    function father(name){
        this.arr = ['aa','bb']
        this.name = name
    }
    father.prototype.showname = function(){
       console.log(this.name)
    }
    function son(name,course){
       father.call(this,name)
       this.course = course
    }
    function extend(subobj,superobj){
       var proobj = Object.create(superobj.prototype)
       subobj.prototype = proobj
    }
    extend(son, father)
    var a = new son('xiaoming', 23)
    console.log(a.arr,a.name,a.course)
    a.showname()
  • 相关阅读:
    bzoj 5028小Z的加油店(D12 序列gcd)(线段树+树状数组)
    蒲公英
    [APIO2012]派遣(可并堆)(D11)
    AT1219 歴史の研究(回滚莫队)
    [USACO05DEC] 布局
    小B的询问
    [HEOI2012]采花(树状数组)(暑假D11)
    [JLOI2011]飞行路线 (暑假D3 拆点+dijkstra堆优化)
    [POI2012]FES-Festival
    [国家集训队]拉拉队排练
  • 原文地址:https://www.cnblogs.com/carry-carry/p/11635007.html
Copyright © 2020-2023  润新知