判断javascript对象的属性是否通过原型链访问的核心方法是:1. 使用 object.hasown(obj, prop) 返回 false 且 prop in obj 返回 true,则属性来自原型链;2. 可通过 object.getprototypeof 递归遍历原型链以定位属性所在原型层级;3. 对于 symbol 属性,同样适用上述方法,但需确保使用正确的 symbol 引用;4. 推荐使用 object.hasown 替代 hasownproperty 以避免对象自身方法被覆盖导致的错误;5. 性能上两者差异可忽略,但高频检测时可缓存 hasownproperty 或使用 object.hasown 提升安全性。该方案完整覆盖属性来源判断、symbol处理、安全性与性能优化,适用于各类原型链属性检测场景。

判断JavaScript对象的属性是否可以通过原型链访问,核心在于检查该属性是否存在于对象自身,而不是其原型链上。简单来说,如果
obj.hasOwnProperty(prop)
false
prop in obj
true

解决方案
要判断一个属性是否是通过原型链访问的,可以使用以下方法:

使用 hasOwnProperty
in
hasOwnProperty
in

function isPropertyFromPrototype(obj, prop) {
return !obj.hasOwnProperty(prop) && (prop in obj);
}
// 示例
function MyObject() {}
MyObject.prototype.protoProp = 'protoValue';
let obj = new MyObject();
obj.ownProp = 'ownValue';
console.log(isPropertyFromPrototype(obj, 'protoProp')); // true
console.log(isPropertyFromPrototype(obj, 'ownProp')); // false
console.log(isPropertyFromPrototype(obj, 'nonExistent')); // false这种方法直接明了,易于理解。但是,它依赖于
hasOwnProperty
in
使用 Object.getPrototypeOf
hasOwnProperty
这种方法稍微复杂一些,但更具灵活性,可以追踪属性来自原型链的哪个层级。
function findPropertyInPrototypeChain(obj, prop) {
let current = obj;
while (current) {
if (current.hasOwnProperty(prop)) {
// 找到属性,但需要判断是否是 obj 自身
if (current === obj) {
return null; // 属性在对象自身上
} else {
return current; // 属性在原型链的某个位置
}
}
current = Object.getPrototypeOf(current);
}
return null; // 属性不存在于原型链上
}
// 示例
function MyObject() {}
MyObject.prototype.protoProp = 'protoValue';
function AnotherObject() {}
AnotherObject.prototype = new MyObject();
AnotherObject.prototype.anotherProtoProp = 'anotherProtoValue';
let obj = new AnotherObject();
obj.ownProp = 'ownValue';
console.log(findPropertyInPrototypeChain(obj, 'protoProp')); // MyObject.prototype
console.log(findPropertyInPrototypeChain(obj, 'anotherProtoProp')); // AnotherObject.prototype
console.log(findPropertyInPrototypeChain(obj, 'ownProp')); // null
console.log(findPropertyInPrototypeChain(obj, 'nonExistent')); // null这种方法允许你不仅判断属性是否来自原型链,还可以确定它来自哪个原型对象。这在调试和理解复杂的原型链结构时非常有用。
Object.hasOwn
hasOwnProperty
Object.hasOwn(obj, prop)
obj.hasOwnProperty(prop)
Object.hasOwn
hasOwnProperty
let obj = {
hasOwnProperty: null // 故意覆盖 hasOwnProperty
};
// obj.hasOwnProperty('prop'); // TypeError: Cannot read properties of null (reading 'call')
Object.hasOwn(obj, 'prop'); // 安全地返回 false使用
Object.hasOwn
hasOwnProperty
如果属性是 Symbol 类型,上述方法仍然适用。
hasOwnProperty
in
const mySymbol = Symbol('mySymbol');
function MyObject() {
this[mySymbol] = 'symbolValue';
}
MyObject.prototype.protoSymbol = Symbol('protoSymbol');
let obj = new MyObject();
console.log(Object.hasOwn(obj, mySymbol)); // true
console.log(mySymbol in obj); // true
console.log(Object.hasOwn(obj, 'protoSymbol')); // false
console.log('protoSymbol' in obj); // false (因为 'protoSymbol' 不是一个 Symbol)
console.log(MyObject.prototype.protoSymbol in obj); // false
console.log(obj.protoSymbol === undefined) // true
const isSymbolFromPrototype = !Object.hasOwn(obj, MyObject.prototype.protoSymbol) && (MyObject.prototype.protoSymbol in obj)
console.log(isSymbolFromPrototype) // false (因为 obj 没有直接继承 MyObject.prototype)
function isSymbolFromPrototype(obj, symbol) {
return !Object.hasOwn(obj, symbol) && (symbol in obj);
}
console.log(isSymbolFromPrototype(obj, MyObject.prototype.protoSymbol)) // false关键在于确保在使用
in
hasOwnProperty
in
通常情况下,
hasOwnProperty
in
hasOwnProperty
Object.hasOwn
const has = Object.prototype.hasOwnProperty; // 缓存 hasOwnProperty
function checkProperties(obj, props) {
for (let i = 0; i < props.length; i++) {
if (has.call(obj, props[i])) {
// ...
}
}
}虽然这种优化在大多数情况下不会带来显著的性能提升,但在特定场景下可能会有所帮助。
以上就是js如何判断属性是否可被原型访问的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号