Javascript 如何检查匿名对象是否有方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3007460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if anonymous object has a method?
提问by Omar
How can I check if an anonymous object that was created as such:
如何检查是否创建了这样的匿名对象:
var myObj = {
prop1: 'no',
prop2: function () { return false; }
}
does indeed have a prop2 defined?
确实有定义的 prop2 吗?
prop2will always be defined as a function, but for some objects it is not required and will not be defined.
prop2将始终定义为函数,但对于某些对象,它不是必需的,也不会被定义。
I tried what was suggested here: How to determine if Native JavaScript Object has a Property/Method?but I don't think it works for anonymous objects .
我尝试了这里的建议:如何确定原生 JavaScript 对象是否具有属性/方法?但我认为它不适用于匿名对象。
回答by Sean Vieira
typeof myObj.prop2 === 'function';will let you know if the function is defined.
typeof myObj.prop2 === 'function';将让您知道该函数是否已定义。
if(typeof myObj.prop2 === 'function') {
alert("It's a function");
} else if (typeof myObj.prop2 === 'undefined') {
alert("It's undefined");
} else {
alert("It's neither undefined nor a function. It's a " + typeof myObj.prop2);
}
回答by artlung
You want hasOwnProperty():
你想要hasOwnProperty():
var myObj1 = {
prop1: 'no',
prop2: function () { return false; }
}
var myObj2 = {
prop1: 'no'
}
console.log(myObj1.hasOwnProperty('prop2')); // returns true
console.log(myObj2.hasOwnProperty('prop2')); // returns false
References: Mozilla, Microsoft, phrogz.net.
参考资料:Mozilla、微软、phrogz.net。
回答by Peter Tseng
3 Options
3 个选项
typeof myObj.prop2 === 'function'if the property name is not dynamic/generatedmyObj.hasOwnProperty('prop2')if the property name is dynamic, and only check if it is direct property (not down the prototype chain)'prop2' in myObjif the property name is dynamic, and check down the prototype chain
typeof myObj.prop2 === 'function'如果属性名称不是动态/生成的myObj.hasOwnProperty('prop2')如果属性名称是动态的,并且只检查它是否是直接属性(而不是原型链的下游)'prop2' in myObj如果属性名称是动态的,请查看原型链
回答by Matt Ball
What do you mean by an "anonymous object?" myObjis not anonymous since you've assigned an object literal to a variable. You can just test this:
“匿名对象”是什么意思?myObj不是匿名的,因为您已将对象文字分配给变量。你可以测试一下:
if (typeof myObj.prop2 === 'function')
{
// do whatever
}
回答by Ain Tohvri
One way to do it must be if (typeof myObj.prop1 != "undefined") {...}
一种方法必须是 if (typeof myObj.prop1 != "undefined") {...}
回答by Javier Elices
I know this is an old question, but I am surprised that all answers ensure that the method exists andit is a function, when the OP does only want to check for existence. To know it is a function (as many have stated) you may use:
我知道这是一个老问题,但是当 OP 只想检查是否存在时,我很惊讶所有答案都确保该方法存在并且它是一个函数。要知道它是一个函数(正如许多人所说),您可以使用:
typeof myObj.prop2 === 'function'
But you may also use as a condition:
但您也可以将其用作条件:
typeof myObj.prop2
Or even:
甚至:
myObj.prop2
This is so because a function evaluates to trueand undefinedevaluates to false. So if you know that if the member exists it may only be a function, you can use:
之所以如此,是因为函数的计算结果为true并且undefined计算结果为false。所以如果你知道如果成员存在它可能只是一个函数,你可以使用:
if(myObj.prop2) {
<we have prop2>
}
Or in an expression:
或者在表达式中:
myObj.prop2 ? <exists computation> : <no prop2 computation>

