Consider:
function Shape() {
this.name = "Generic";
this.draw = function() {
return "Drawing " + this.name + " Shape";
};
}
function welcomeMessage()
{
var shape1 = new Shape();
//alert(shape1.draw());
alert(shape1.hasOwnProperty(name)); // This is returning false
}
.welcomeMessage called on the body.onload event.
I expected shape1.hasOwnProperty(name) to return true, but it’s returning false.
What is the correct behavior?
hasOwnPropertyis a normal JavaScript function that takes a string argument.When you call
shape1.hasOwnProperty(name)you are passing it the value of thenamevariable (which doesn’t exist), just as it would if you wrotealert(name).You need to call
hasOwnPropertywith a string containingname, like this:shape1.hasOwnProperty("name").