当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。