當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Javascript hasOwnProperty()用法及代碼示例


JavaScript中的hasOwnProperty()方法用於檢查對象是否具有指定的屬性作為其自身的屬性。這對於檢查對象是否繼承了屬性而不是屬性是很有用的。

用法:

object.hasOwnProperty( prop )

參數:此方法接受單個參數prop,該屬性以字符串或要測試的屬性的Symbol形式保存名稱。


返回值:它返回一個布爾值,指示對象是否具有給定的屬性作為其自己的屬性。

範例1:本示例檢查對象的屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        JavaScript | hasOwnProperty() Method 
    </title> 
</head> 
  
<body> 
    <h1 style="color:green"> 
        GeeksforGeeks 
    </h1> 
  
    <b> 
        hasOwnProperty() method in JavaScript 
    </b> 
      
    <p> 
        Click on the button to check if the 
        properites are of the object. 
    </p> 
      
    <p>Output for own property:  
        <span class="outputProperty"></span> 
    </p> 
      
    <p>Output for not own property:  
        <span class="outputNonProperty"></span> 
    </p> 
  
    <button onclick="checkProperty()"> 
        Check properties 
    </button> 
      
    <script type="text/javascript"> 
  
        function checkProperty() { 
            let exampleObj = {}; 
            exampleObj.height = 100; 
            exampleObj.width = 100; 
  
            // Checking for existing property 
            result1 = exampleObj.hasOwnProperty("height"); 
  
            // Checking for non-existing property 
            result2 = exampleObj.hasOwnProperty("breadth"); 
  
            document.querySelector(".outputProperty").textContent 
                        = result1; 
              
            document.querySelector(".outputNonProperty").textContent 
                        = result2; 
        } 
    </script> 
</body> 
  
</html>

輸出:

  • 在單擊按鈕之前:
    object-before
  • 單擊按鈕後:
    object-after

範例2:本示例檢查類的對象的屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        JavaScript hasOwnProperty() method 
    </title> 
</head> 
  
<body> 
    <h1 style="color:green"> 
        GeeksforGeeks 
    </h1> 
      
    <b> 
        hasOwnProperty() method in JavaScript 
    </b> 
      
    <p> 
        Click on the button to check if the 
        properites are of the object. 
    </p> 
      
    <p>Output for own property:  
        <span class="outputProperty"></span> 
    </p> 
      
    <p>Output for not own property:  
        <span class="outputNonProperty"></span> 
    </p> 
  
    <button onclick="checkProperty()"> 
        Check properties 
    </button> 
      
    <script type="text/javascript"> 
  
        function checkProperty() { 
              
            function Car(a, b) { 
                this.model = a; 
                this.name = b; 
            } 
  
            let car1 = new Car("Mazda", "Laputa"); 
  
            // Checking for existing property 
            result1 = car1.hasOwnProperty("model"); 
  
            // Checking for non-existing property 
            result2 = car1.hasOwnProperty("wheels"); 
  
            document.querySelector(".outputProperty").textContent 
                        = result1; 
              
            document.querySelector(".outputNonProperty").textContent 
                        = result2; 
        } 
    </script> 
</body> 
  
</html>

輸出:

  • 在單擊按鈕之前:
    class-object-before
  • 單擊按鈕後:
    class-object-after

支持的瀏覽器:下麵列出了JavaScript hasOwnProperty()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • Firefox 1
  • IE瀏覽器
  • 邊12
  • 蘋果瀏覽器
  • Opera


相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 JavaScript | hasOwnProperty() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。