原型和setTimeous

时间:2016-10-06 19:26:48

标签: javascript prototypejs

setTimeout在以下代码中不起作用。

我该如何解决?

function Human(name, surname, sex) {
    this.name = name;
    this.surname = surname;
    this.sex = sex;
};

Human.prototype.wash = function() {
    console.log(this.sex + ' ' + this.name + this.surname + ' ' + 'takes a cleaner and start washing')
}

Human.prototype.washing = function() {
    var that = this;
    setTimeout(function() {
        console.log(that.name + 'still washing...'), 3000
    });
};


function Human1(name, surname, sex) {
    Human.apply(this, arguments);
};


Human1.prototype = Object.create(Human.prototype);
Human1.prototype.constructor = Human1;

Human1.prototype.wash = function() {
    Human.prototype.wash.apply(this);
    Human.prototype.washing.apply(this);
    console.log(this.name);
};

var Andrey = new Human1('Andrey', 'Balabukha', 'male');
Andrey.wash();

1 个答案:

答案 0 :(得分:0)

超时时间错误。应该是:

Human.prototype.washing = function() {
    var that = this;
    setTimeout(function() {
        console.log(that.name + 'still washing...');
    }, 3000);
};