.hasOwnProperty()是用于检查对象是否拥有指定属性的方法
|
hasOwnProperty 是 JavaScript 中用于检查对象是否拥有指定属性的方法,属于 Object.prototype 的内置方法。该方法仅检查对象自身的属性(非继承自原型链的属性),并返回布尔值。
基本用法:
语法:obj.hasOwnProperty(prop)
参数:
obj:需检查的对象
prop:需检查的属性名
返回值:
若对象自身包含该属性,返回 true
若属性为继承或不存在,返回 false
示例:
- const person = { name: 'John', age: 30 };
- console.log(person.hasOwnProperty('name')); // true(自身属性)
- console.log(person.hasOwnProperty('age')); // true(自身属性)
- console.log(person.hasOwnProperty('gender')); // false(未定义属性) :ml-citation{ref="1,2" data="citationList"}
复制代码
|
|
|
|
|
|
|
|