如何判定一个属性来自于对象本身, 还是来自于原型链【热度: 224】

关键词:对象与原型链

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

一、使用 hasOwnProperty() 方法

  1. 方法介绍:

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

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

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

  1. 方法介绍:

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

    function Person() {}
    Person.prototype.name = "prototype name";
    
    const person = new Person();
    person.age = 30;
    
    const propertyName = "name";
    if (person.hasOwnProperty(propertyName)) {
      console.log(`${propertyName} is an own property of the object.`);
    } else if (propertyName in person) {
      console.log(`${propertyName} is inherited from the prototype.`);
    } else {
      console.log(`${propertyName} is not found in the object or its prototype.`);
    }

三、使用 Object.getOwnPropertyDescriptor() 方法

  1. 方法介绍:

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

    function Person() {}
    Person.prototype.name = "prototype name";
    
    const person = new Person();
    person.age = 30;
    
    const ageDescriptor = Object.getOwnPropertyDescriptor(person, "age");
    const nameDescriptor = Object.getOwnPropertyDescriptor(person, "name");
    
    if (ageDescriptor) {
      console.log("age is an own property of the object.");
    }
    if (!nameDescriptor) {
      console.log("name is not an own property of the object.");
    }