hasOwnProperty and in attribute operations in JavaScript

Keywords: Attribute

1.in operator:

Used to check whether an attribute exists in an object (without distinguishing between instance attributes and prototype attributes)

<script>

    //01 Provides a constructor
    function Person(name) {
        this.name = name;
    }

    //02 Sets the properties of the prototype object of the constructor
    Person.prototype.sayHello = function () {
        console.log("hello");
    }

    //03 Create Objects
    var p1 = new Person();

    //04 Use the in keyword to determine whether there are the following attributes in the object: name age sayHello
    console.log("age" in p1);       //false
    console.log("name" in p1);      //true
    console.log("sayHello" in p1);  //true

</script>

2.hasOwnProperty Method

<script>

    //01 Provides a constructor
    function Person(name) {
        this.name = name;
    }

    //02 Sets the properties of the prototype object of the constructor
    Person.prototype.sayHello = function () {
        console.log("hello");
    }

    Person.prototype.des = "Default description information";

    //03 Create Objects
    var p1 = new Person();

    //hasOwnProperty method is used to determine whether the attribute is an instance attribute of an object.
    console.log(p1.hasOwnProperty("age"));       //false
    console.log(p1.hasOwnProperty("name"));      //true
    console.log(p1.hasOwnProperty("sayHello"));  //false
    console.log(p1.hasOwnProperty("des"));       //false

</script>

Conclusion:
Description: In keyword is used to check whether there are specified attributes (including instance attributes and prototype attributes) in the object.
The hasOwnProperty method of an object is used to check whether a specified attribute exists in the object (only instance attributes are checked)

3. Whether an attribute exists and only exists in prototype attributes

Solutions:
Attributes exist in objects
Attributes are not instance attributes

<script>

    //01 Provides a constructor
    function Person(name) {
        this.name = name;
    }

    //02 Sets the properties of the prototype object of the constructor
    Person.prototype.sayHello = function () {
        console.log("hello");
    };

    Person.prototype.des = "Default description information";

    //03 Create Objects
    var p1 = new Person();


    function isProperty(obj, property) {
        return !obj.hasOwnProperty(property) && (property in obj);
    }

    console.log(isProperty(p1, "constructor"));    //true
    console.log(isProperty(p1, "age"));            //false
    console.log(isProperty(p1, "name"));           //false
    console.log(isProperty(p1, "des"));            //true
    console.log(isProperty(p1, "sayHello"));        //true
    console.log(p1);

</script>

4.delete

By using the delete operator, instance attributes can be completely deleted, thus enabling us to consolidate the re-access of attributes in the prototype.

function Person() {
  this.name = 'koewas';
}
    
// Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function() {
  console.log(this.name);
};

let person1 = new Person();
let person2 = new Person();

console.log(person1.hasOwnProperty("name"));    // true
console.log("name" in person1);    // true

person1.name = "Greg";

console.log(person1.name);    // "Greg"
console.log(person1.hasOwnProperty("name"));    // true
console.log("name" in person1);    // true

delete person1.name;

console.log(person1.name);    // undefined
console.log(person1.hasOwnProperty("name"));    // false
console.log("name" in person1);    // false

The name of the above code is an instance attribute, not a prototype. But it is not a private property and can be accessed by instances.

Reference article:
https://www.jianshu.com/p/2e30dcb0ef77
https://www.cnblogs.com/weiqinl/p/8683207.html
https://blog.csdn.net/JimmyLuo17/article/details/75252384
https://www.jianshu.com/p/c63de804f2bd

Posted by Stuph on Sun, 31 Mar 2019 14:12:29 -0700