Skip to content
Advertisements

Object.prototype.hasOwnProperty()

Why Object.prototype.hasOwnProperty ?

The Object.hasOwnProperty () method checks whether an object has the desired property in it. It returns boolean values ​​as a result. If False returns, the object does not have the desired property. If True returns, the object has the desired property.
The difference between the Object.hasOwnProperty () method and the operator “in”; Object.hasOwnProperty () does not check a property in the object’s prototype chain.

Syntax:

Object.hasOwnProperty( prop )

  • prop: A string or symbol to test.

Return value:

A Boolean indicating whether or not the object has the specified property as own property.


JavaScript Object.prototype.hasOwnProperty Examples ?

Example 1

        let myObj = {
            name: "rick",
            print: function () {
                return "name: " + this.name;
            }
        };

        console.log(myObj.hasOwnProperty(name));
        console.log(myObj.hasOwnProperty(print));
        console.log(myObj.hasOwnProperty(print.name));

output:

false
false
true


Example 2

        var people = {
            person1: 'alex',
            person2: 'maria',
        }

        var from = {
            perFrom: 'England',
        }

        let arr1 = people.hasOwnProperty('person1');
        let arr2 = people.hasOwnProperty('person2');

        let arr3 = from.hasOwnProperty('perFrom');
        let arr4 = from.hasOwnProperty('person1');
        let arr5 = from.hasOwnProperty('person2');

        console.log(arr1);
        console.log(arr2);
        console.log(arr3);
        console.log(arr4);
        console.log(arr5);

output:

true
true
true
false
false


Exampe 3


        let obj = {
            "date": 1354
        };

        let myObj = Object.create(obj);

        console.log(myObj.hasOwnProperty("date"));
        console.log("date" in obj);

output:

false
true


Example 4


        myObj = new Object();

        myObj.prop1 = null;
        let arr1 = myObj.hasOwnProperty("prop1");
        console.log(arr1);

        myObj.prop2 = undefined;
        let arr2 = myObj.hasOwnProperty("prop2");
        console.log(arr2);

output:

true
true


Example 5


        var info = [
            "Iron Man 3",
            "Star Trek: Into Darkness",
            "Man of Steel"
        ];

        let arr1 = info.hasOwnProperty("1");
        let arr2 = info.hasOwnProperty("2");
        let arr3 = info.hasOwnProperty("3");

        console.log(arr1)
        console.log(arr2)
        console.log(arr3)

output:

true
true
false


Example 6

For this example, you need to know the “object” topics:

javascript object example
<input type="text" id="username" />
    <input type="text" id="password" />
    <button onclick="login();">LOGİN</button>


    <script>


        function login() {
            var users = {
                "alex": 123,
                "rick": 234,
            };
            let username = document.getElementById("username").value,
                password = document.getElementById("password").value;

            if (users.hasOwnProperty(username) == true) {
                let arr1 = Object.getOwnPropertyDescriptor(users, "alex");
                let arr2 = Object.getOwnPropertyDescriptor(users, "rick");

                if (arr1.value == password) {
                    alert("welcome " + username);
                } else if (arr2.value == password) {
                    alert("welcome " + username);
                }else {
                    alert("You are entering the password incorrectly.")
        }
            } else {
                alet("You are entering the username incorrectly.")
            }
        }
    

    </script>

Sources:


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes
Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: