I have a test which looks like this:
- Create a base class
Human
Human class
needs to have a methodTalk
- The
Human
class needs to have to descendant classMan
andWoman
- Both
Man
andWoman
should have their ownTalk
method (basically rewrite the method) - The
Man
class should have aprivate
property_foo
- It should have a method
getInfo
(which needs to be an Ajax call and log the response) - I need to make 1000 instances of the
Women
class in thewindow
namespace (I mean global) - On
document.body
single click, a randomWoman
should call theTalk
method - On
document.body
double click, theMan
'sgetInfo
method should be called
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();
}
1 Answer 1
Some opinions:
function Woman(){ Human.call(this); } Woman.prototype = new Human();
Don't use new
for prototype (even when it doesn't matter here as long as Human
is empty, but the Huma.call(this)
suggests otherwise). Check https://stackoverflow.com/q/10898786/1048572
// Woman subClass function Man(){
Apparently not.
var _foo = 10;
It's a local-scoped ("private") variable anyway, you don't need to prefix it with an underscore. Notice that it is only accessible from functions declared within the constructor, of which you don't have any.
return Human.call(this);
No reason to return
anything here. It might even be wrong it Human
did return an object.
Man.prototype.getInfo = function(method, url){ [...] }
This doesn't seem to have anything to do with instances of Man
, so I wonder why it is a method? But that's probably just the design of your test case.
console.log('man private variable _foo: ' + (man._foo));
Not sure what you did expect, but yes, since _foo
has been a variable and not a property of the object so this is undefined
.
document.body.onclick = function(){ [...]
...is missing a closing brace.
var randNr = Math.floor((Math.random()*1000)+1); womans[randNr].talk();
No. That randNr
will be in the range 1-1000 (both inclusive), while your array indices are 0-999. With a small chance, this will throw an exception. Remove the +1
.
console.log('Random Woman: ' + randNr);
...and add it here if you want that output one-based.
Man.prototype.getInfo();
That confirms what I said above - you don't use an instance for that method. You should hardly ever call methods on the prototype object - man.getInfo()
would be better. But if you really intended this to be a static function, you might write Man.getInfo = function() {...};
and then call Man.getInfo();
.
-
\$\begingroup\$ I edited the code, please could you take a second and check if now the code is suffice for the test above. i would highly appreciate it. \$\endgroup\$Mr. Sam– Mr. Sam2014年01月30日 08:19:02 +00:00Commented Jan 30, 2014 at 8:19
-
1\$\begingroup\$ Uh, actually I had only looked at the code since you can check whether it meets your expectatations by simply executing it :-) I'd say it does what is described, the only term that striked me was "private property" as such by definition don't exist :-) \$\endgroup\$Bergi– Bergi2014年01月30日 13:51:02 +00:00Commented Jan 30, 2014 at 13:51