温馨提示×

hasOwnProperty有没有替代方法

小樊
86
2024-06-19 16:55:01
栏目: 深度学习

有,可以使用Object.prototype.hasOwnProperty.call()方法来替代hasOwnProperty方法。例如:

var obj = {
  key: 'value'
};

if (Object.prototype.hasOwnProperty.call(obj, 'key')) {
  console.log('The key exists in the object.');
} else {
  console.log('The key does not exist in the object.');
}

0