прототип и время установки

#javascript #prototypejs

#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. как это должно работать? что вы ожидаете здесь увидеть console.log(this.name); ? и в вашем setTimeout вы поставили 3000 не в то место

Ответ №1:

Время ожидания указано не в том месте. Должно быть:

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