zoukankan      html  css  js  c++  java
  • 学习笔记之--初探prototype

    // 构造函数/基类
    function Human(name){
    this.name = name;
    }

    //将基类的方法保存在构造函数的prototype属性中
    //便于子类的继承
    Human.prototype.say=function(){
    console.log('say');
    }

    //道格拉斯的object方法(等同于object.create方法)
    function object(o){
    var F = function(){};
    F.prototype = o;
    return new F();
    }

    //子类构造器
    function Man(name,age){
    //调用父类的构造函数
    Human.call(this,name);
    //自己属性的age
    this.age = age;
    }

    //继承父类的方法 constructor 属性返回对创建此对象的数组函数的引用。
    Man.prototype = object(Human,prototype);
    Man.prototype.constructor = Man;

    //实例化子类
    var man = new Man("Lee", 22);
    console.log(man);
    //调用父类的say方法;
    man.say();

  • 相关阅读:
    1月19日
    1月18日
    1月17日
    读后感(1)
    UIAlertView
    plist
    jQuery validation
    HTML <a href >标签的target属性
    HTML DOM
    .与..的区别
  • 原文地址:https://www.cnblogs.com/zhangtinghang/p/8303053.html
Copyright © 2011-2022 走看看