如果对象原型包含特定方法,我想在虚拟对象/实例上覆盖它。我希望以递归方式对此对象内的所有属性,对象和数组执行此操作。
检查对象原型是否为其提供方法的正确方法是什么?
if('myInterestedMethod' in obj1){
if(!obj1.hasOwnProperty('myInterestedMethod'){
console.log('Method is from its prototype');
}
}
答案 0 :(得分:1)
执行for-in
迭代属性并检查每个键的值是否为function
for ( var prop in obj1 )
{
if( !obj1.hasOwnProperty( prop ) && typeof obj1[ prop ] == "function" )
{
console.log( prop, 'Method is from its prototype');
}
}