zoukankan      html  css  js  c++  java
  • 构造函数的继承

    // 构造函数
    function Animal() {
    this.species = "动物";
    }
    function Cat(name, color) {
    this.name = name;
       this.color = color;
    }

    // 1, 使用call或者apply方法,将父对象的构造函数绑定在子对象上,即在子对象构造函数中加一行
    // function Cat(name, color) {
    // Animal.apply(this, arguments);
    // this.name = name;
    // this.color = color;
    // }
    // var cat1 = new Cat('大猫','黄色');
    // console.log(cat1);


    // 2. 第二种 prototype属性
    // 第一行,将prototype对象指向一个Animal的实例
    // Cat.prototype = new Animal();
    // 第二行,constructor属性,指向它的构造函数,
    // 如果没有Cat.prototype = new Animal(),Cat.prototype.constructor指向Cat
    // 现在指向Cat.prototype.constructor指向Animal
    // Cat.prototype.constructor = Cat;
     
    // var cat1 = new Cat('大毛','黄色');
    // console.log(cat1);

    // 3.直接继承prototype
    // function Animal() {
     
    // }
    // Animal.prototype.species = "动物";
    // Cat.prototype = Animal.prototype;
    // Cat.prototype.constructor = Cat;
    // var cat1 = new Cat('大毛','黄色');
    // console.log(cat1.species);

    // 5. 拷贝继承
    // function Animal() {}
    // Animal.prototype.species = "动物";
    // function extend(Child, Parent) {
    // var p = Parent.prototype;
    // var c = Child.prototype;
    // for(var i in p) {
    // c[i] = p[i];
    // }
    // c.uber = p;
    // }
    // extend(Cat,Animal);
    // var cat1 = new Cat('大毛','黄色');
    // console.log(cat1);

     摘自阮老师的博客:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html

  • 相关阅读:
    HTML 与 HTML 页面之间动态传值的问题
    maven 导入本地项目(JQuery中的绝杀 $("表单").serialize() 可以自动提交表格数据)+邮件发送+通用的Servlet写法
    linux服务器nginx的卸载
    http协议
    所谓的批量删除
    查看本机ssh公钥,生成公钥
    centos7 redis5编译安装
    linux没有ll等命令的解决办法
    Linux 安装python3.7.0
    CentOS7 安装mysql
  • 原文地址:https://www.cnblogs.com/neilniu/p/10739560.html
Copyright © 2011-2022 走看看