I have a test which looks like this:
- create a base class Human
- Human class needs to have a method Talk
- the Human class needs to have to descendant class Man and Woman
- both Man and Woman should have their own Talk method (basically rewrite the method)
- the Man class should have a private property _foo
- Also it should have a method getInfo (which needs to be an ajax call, an log the response)
- I need to make 1000 instances of the Women class in the window namespace (i mean global)
- on document.body single click a random women should call the talk method
- on document.body double click the Man getInfo method should be called
So I now there are plenty of stuff but please bear with me. I already wrote the code, i would just like someone to check it out say an opinion.
I'm very grateful for any hopeful response. So here's the code:
function Human(){};
Human.prototype.talk = function(){
console.log('Make an Human sound');
}
function Woman(){
Human.call(this);
}
Woman.prototype = Object.create(Human.prototype);
Woman.prototype.constructor = Woman;
Woman.prototype.talk = function(){
console.log('Miau');
}
function Man(){
var foo = 10;
Human.call(this);
}
Man.prototype = Object.create(Human.prototype);
Man.prototype.constructor = Man;
Man.prototype.talk = function(){
console.log('Wuff');
}
Man.prototype.getInfo = function(method, url){
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.send(null);
xhr.onreadystatechange = function() {
console.log('Ajax response: ' + xhr.readyState);
};
}
var woman = new Woman();
console.log('woman instance of Woman ' + (woman instanceof Woman));
console.log('woman instance of Human ' + (woman instanceof Human));
woman.talk();
var man = new Man();
console.log('man instance of Man ' + (man instanceof Man));
console.log('man instance of Human ' + (man instanceof Human));
man.talk();
womans = [];
for(var i = 0; i < 1000; i++) {
womans[i] = new Woman();
}
document.body.onclick = function(){
var randNr = Math.floor((Math.random()*1000)+1);
womans[randNr].talk();
console.log('Random Woman: ' + randNr);
}
document.body.ondblclick = function(){
Man.prototype.getInfo();
}