zoukankan      html  css  js  c++  java
  • JS继承

    JS继承

    伪类:通过构造一个人伪类来继承某个构造器。通过定义它的constructor函数并替换它的prototype为某个构造器的实例。(类的继承)

    //构造器
    var Mammal = function (name) {
        this.name = name;    
    }
    Mammal.prototype.get_name = function () {
        return this.name;  
    }
    Mammal.prototype.says = function () {
        return this.saying;  
    }
    //继承
    var Cat = function (name) {
        this.name = name;
        this.saying = 'meow';      
    }
    //替换Cat.prototype为一个新的Mammal实例
    Cat.prototype = new Mammal();

    原型:新对象继承旧对象,通过创建一个对象并将其prototype指向目标原型对象。

    // Object.creat
        Object.creat =function (o) {
            var F= function () {};
            F.prototype=o;
            return new F();     
        }
    //对象
    var myMammal ={
        name : ‘ bss’,
        get_name : function (){
            return this.name;
    },
        says : function () {
            return this.saying || ' ';
    },
    }   
    //继承对象
    var myCat = Object.creat(myMammal);
  • 相关阅读:
    CSU L: 就多了两分钟
    CSU 1112【机器人的指令】模拟
    P3388 【模板】割点(割顶)
    go 学习 2
    go 学习 1
    netconf协议
    lua 学习 5
    lua 学习 4
    lua 学习 3
    lua 学习 2
  • 原文地址:https://www.cnblogs.com/JhonFlame/p/8045809.html
Copyright © 2011-2022 走看看