关键词:对象与原型链

在 JavaScript 中,可以通过以下几种方式来判断一个属性是来自对象本身还是来自原型链:

一、使用 hasOwnProperty() 方法

  1. 方法介绍:

    • hasOwnProperty()是 JavaScript 对象的一个方法,用于判断一个对象自身是否具有指定的属性。
    • 它不会检查原型链上的属性,只关注对象本身是否拥有该属性。
  2. 示例代码:

     1function Person() {}
     2Person.prototype.name = "prototype name";
     3
     4const person = new Person();
     5person.age = 30;
     6
     7console.log(person.hasOwnProperty("age")); // true,说明 age 属性是对象本身的属性
     8console.log(person.hasOwnProperty("name")); // false,说明 name 属性不在对象本身,而是在原型链上

二、使用 in 操作符结合 hasOwnProperty()

  1. 方法介绍:

    • in操作符用于检查一个对象及其原型链中是否具有指定的属性。
    • 可以结合hasOwnProperty()来判断属性的来源。
  2. 示例代码:

     1function Person() {}
     2Person.prototype.name = "prototype name";
     3
     4const person = new Person();
     5person.age = 30;
     6
     7const propertyName = "name";
     8if (person.hasOwnProperty(propertyName)) {
     9  console.log(`${propertyName} is an own property of the object.`);
    10} else if (propertyName in person) {
    11  console.log(`${propertyName} is inherited from the prototype.`);
    12} else {
    13  console.log(`${propertyName} is not found in the object or its prototype.`);
    14}

三、使用 Object.getOwnPropertyDescriptor() 方法

  1. 方法介绍:

    • Object.getOwnPropertyDescriptor()方法返回指定对象上一个自有属性的属性描述符。
    • 如果对象没有指定的自有属性,则返回undefined
  2. 示例代码:

     1function Person() {}
     2Person.prototype.name = "prototype name";
     3
     4const person = new Person();
     5person.age = 30;
     6
     7const ageDescriptor = Object.getOwnPropertyDescriptor(person, "age");
     8const nameDescriptor = Object.getOwnPropertyDescriptor(person, "name");
     9
    10if (ageDescriptor) {
    11  console.log("age is an own property of the object.");
    12}
    13if (!nameDescriptor) {
    14  console.log("name is not an own property of the object.");
    15}
个人笔记记录 2021 ~ 2025