I am trying to learn object oriented javascript. Why is this call to an object method not working (no alert)?
When you click on the link, an alert is supposed to appear stating how old Bob is.
In case the link ever disappears, here is the HTML:
<a href="#" onclick="bob.say()">eh</a>
and here is my javascript.
function guy(person_name) {
this.name = person_name;
this.age = 32;
this.say = function() {
alert(this.name + " is " + this.age);
return false;
}
}
var bob = new guy("Bob");
Chrome Web Developer Console states
Uncaught ReferenceError: bob is not defined
However, I think I defined bob with var bob = new guy("Bob");.
You are missing the closing
}onsay().Beyond that, you must not return a value from the object constructor function. Doing so will simply set the variable
bobtofalse.Note also that JavaScript conventions typically dicatate that an object constructor be capitalized like
function Guy()Here is the updated fiddle.