//人类
    function Person(name) {
        this.name = name;
        this.showName = function () {
            console.log("my name is " + name);
        }
        this.eat = function () {
            console.log("人是铁饭是钢...");
        }
    }
    //白人
    function WhitePerson(name) {
        this.temp = Person;
        this.temp(name);
        delete this.temp;
        this.color = function () {
            console.log("我们皮肤是偏白色的");
        }
    }
    //黑人
    function BlackPerson(name) {
        Person.call(this, name);//这个时候Person中的this指向的是BlackPerson的对象了
        this.color = function () {
            console.log("我们皮肤是偏黑色的");
        }
    }
    var wPreson = new WhitePerson("tom");
    wPreson.showName();
    var bPerson = new BlackPerson("john");
    bPerson.showName(); 

以对象冒充的方式来实现js的继承

相关文章:

  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2021-08-06
  • 2021-12-15
  • 2022-12-23
  • 2021-04-19
  • 2021-05-30
猜你喜欢
  • 2021-12-09
  • 2021-09-15
  • 2021-04-17
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案