问题描述
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
在 body.onload
事件中调用。
我预计 shape1 .hasOwnProperty(name)
返回true,但返回false。
I expected shape1.hasOwnProperty(name)
to return true, but its returning false.
什么是正确的行为?
推荐答案
hasOwnProperty
是一个普通的Javascript函数,它接受一个字符串参数。
hasOwnProperty
is a normal Javascript function that takes a string argument.
当你调用 shape1.hasOwnProperty(name )
你传递的是 name
变量的值(它不存在),就像你写的那样 alert(name)
。
When you call shape1.hasOwnProperty(name)
you are passing it the value of the name
variable (which doesn't exist), just as it would if you wrote alert(name)
.
您需要使用字符串调用 hasOwnProperty
包含 name
,如下所示: shape1.hasOwnProperty(name)
。
You need to call hasOwnProperty
with a string containing name
, like this: shape1.hasOwnProperty("name")
.
这篇关于在javascript中的hasOwnProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!