2016-10-06 7 views
-1

setTimeoutは、以下のコードでは機能しません。プロトタイプおよびsetTimeous

どうすれば修正できますか?

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(); 
+0

どうすればいいですか?あなたはここで 'console.log(this.name);に何を期待しますか?'あなたの 'setTimeout'では' 3000'を間違った場所に入れます –

答えて

0

タイムアウトが間違っています。する必要があります:

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