对象的原型链是javascript中用于查找属性和方法的路径,当对象自身无该属性时,会向上遍历原型链直至null。1. 获取原型的标准方法是object.getprototypeof(obj),返回对象的内部[[prototype]];2. 非标准但广泛支持的__proto__也可访问原型,但推荐优先使用标准方法;3. 判断某对象是否在另一对象原型链中,可用a.isprototypeof(b)返回布尔值;4. 原型链顶端为null,object.getprototypeof(object.prototype)结果为null;5. object.create(null)创建无原型对象,不继承任何属性方法,适用于纯净字典场景;6. 原型链是javascript继承的核心,通过原型指向实现属性方法共享,如构造函数结合object.create实现继承;7. 修改原型会影响所有继承该原型的实例,因其共享同一原型对象,需谨慎操作以避免副作用。

对象的原型链,简单来说,就是对象查找属性和方法的路径。当你在一个对象上访问一个属性或方法时,如果该对象自身没有,JavaScript 引擎就会沿着它的原型链向上查找,直到找到为止,或者到达原型链的顶端(
null

获取对象原型链的方法,最常用的就是通过
Object.getPrototypeOf()
__proto__
Object.getPrototypeOf(obj)
[[Prototype]]

const myObj = {};
const prototype = Object.getPrototypeOf(myObj);
console.log(prototype); // 输出: [Object: null prototype] {} (Object.prototype)obj.__proto__
[[Prototype]]
Object.getPrototypeOf()
const myObj = {};
const prototype = myObj.__proto__;
console.log(prototype); // 输出: [Object: null prototype] {} (Object.prototype)可以使用
Object.prototype.isPrototypeOf()

const parent = {};
const child = Object.create(parent); // 创建一个以 parent 为原型的对象
console.log(parent.isPrototypeOf(child)); // 输出: true
console.log(child.isPrototypeOf(parent)); // 输出: false原型链的顶端是
null
null
const myObj = {};
const prototype = Object.getPrototypeOf(myObj);
console.log(Object.getPrototypeOf(prototype)); // 输出: nullObject.create(null)
Object.create(null)
toString()
hasOwnProperty()
const myMap = Object.create(null);
myMap.key1 = 'value1';
console.log(myMap.key1); // 输出: value1
console.log(myMap.toString); // 输出: undefined (因为没有原型)
// 安全地检查属性是否存在,避免原型链污染
if (Object.prototype.hasOwnProperty.call(myMap, 'key1')) {
console.log('key1 exists'); // 输出: key1 exists
}原型链是 JavaScript 实现继承的核心机制。 当一个对象继承自另一个对象时,实际上是让该对象的原型指向另一个对象。 这样,子对象就可以访问父对象的属性和方法。 例如,使用
Object.create()
function Animal(name) {
this.name = name;
}
Animal.prototype.sayHello = function() {
console.log(`Hello, I'm ${this.name}`);
};
function Dog(name, breed) {
Animal.call(this, name); // 调用父构造函数
this.breed = breed;
}
Dog.prototype = Object.create(Animal.prototype); // 设置原型链
Dog.prototype.constructor = Dog; // 修正 constructor 属性
Dog.prototype.bark = function() {
console.log('Woof!');
};
const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.sayHello(); // 输出: Hello, I'm Buddy (继承自 Animal)
myDog.bark(); // 输出: Woof! (Dog 自己的方法)
console.log(myDog instanceof Animal); // 输出: true
console.log(myDog instanceof Dog); // 输出: true是的。 如果修改了对象的原型,所有继承自该原型的对象都会受到影响。 这是因为它们共享同一个原型对象。 需要谨慎修改原型,避免意外地影响其他对象。
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log(`Hi, I'm ${this.name}`);
};
const person1 = new Person('Alice');
const person2 = new Person('Bob');
person1.greet(); // 输出: Hi, I'm Alice
person2.greet(); // 输出: Hi, I'm Bob
// 修改原型
Person.prototype.greet = function() {
console.log(`Greetings, ${this.name}!`);
};
person1.greet(); // 输出: Greetings, Alice!
person2.greet(); // 输出: Greetings, Bob! (person2 也受到了影响)以上就是js中如何获取对象的原型链的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号