I am trying to call a method from JavaScript from an HTML file. Specifically, call the method "speak" from Dog and Cat (shown below the HTML). I think I should be using window.onload = function()
or something similar, with onload
, but I do not know how to call the methods.
This is the HTML content:
<!DOCTYPE html>
<html>
<head>
<script src="Ej6.js"></script>
<script>
window.onload = function() {
}
</script>
</head>
<body>
</body>
</html>
And this is my JavaScript code where the functions I want to call are:
function Animal(name, eyeColor) {
this.name = name;
this.eyeColor = eyeColor;
}
Animal.prototype.getName=function() {
return this.name;
};
Animal.prototype.getEyeColor=function() {
return this.eyeColor;
};
Animal.prototype.toString=function() {
return this.name + " " +
this.eyeColor;
};
function Dog(name, eyeColor) {
Animal.call(this, name, eyeColor);
}
Dog.prototype = new Animal();
Dog.prototype.toString=function() {
return Animal.prototype.toString.call(this);
};
Dog.prototype.speak=function() {
return "woof";
};
function Cat(name, eyeColor) {
Animal.call(this, name, eyeColor);
}
Cat.prototype = new Animal();
Cat.prototype.toString=function() {
return Animal.prototype.toString.call(this);
};
Cat.prototype.speak=function() {
return "meow";
};