I'm having an issue with Javascript object literals.
I would like to reference the object within one of the functions:
var Obj = {
name : "Johnny",
dumb : function() {
alert(this.name);
}
}
Sadly, the "dumb" function is an object as well. So, since dumb() has no 'name' property, it will return as undefined.
How do I get around this?
dumb
is a method on your Obj object. When called, this
will be set to Obj
, and will alert "Johnny"
Try it out
var Obj = {
name : "Johnny",
dumb : function() {
alert(this.name);
}
}
Obj.dumb();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With