zoukankan      html  css  js  c++  java
  • 297 hasOwnProperty 与 in

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <script>
            // hasOwnProperty  
            // 语法: 对象.hasOwnProperty("属性名");
            // 该方法的作用: 可以检测一个属性是否是对象自身的,如果是,返回true,否则返回false
    
            // hasOwnProperty 是 Object.prototype原型上的方法
            // console.log(Object.prototype);
    
    
            function Person(name){
                this.name = name;
            }
    
            Person.prototype.gender = "male";
    
            var p = new Person("zs");
    
            // console.log(p);
            console.log(p.hasOwnProperty("name"));  // true
            console.log(p.hasOwnProperty("gender"));  // false
            
            // 检测p对象自身有没有 hasOwnProperty 属性
            // 该方法是来源于 Object.prototype 原型上的
            // p实例对象可以访问 hasOwnProperty 该方法
            console.log(p.hasOwnProperty("hasOwnProperty"));  // false
    
            // 数组本身没有push / pop 等方法,这些方法都是存在于数组的原型上(Array.prototype),[] 是可以访问数组原型上的这些方法的。
            console.log([].hasOwnProperty("push")); // false 
    
    
            /*小结:
            hasOwnProperty 在 Object.prototype 原型上
            语法: 对象.hasOwnProperty("属性名");
            作用: 是检测属性是否是对象自身的。*/
        </script>
    </body>
    </html>
    

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <script>
            /*hasOwnProperty 与 in 比较
    
            in  在 for...in中有使用
            in 单独使用
            语法: 属性名 in 对象
            作用: 检测对象是否可以使用该属性,如果可以,返回true
                  无论该属性是对象自身的还是原型链上的,只要能够使用到,都返回true.
            hasOwnProperty 作用: 判断属性是否是自身的。*/
    
    
            function Person(name){
                this.name = name;
            }
    
            Person.prototype.gender = "male";
            Object.prototype.sex = "female";
    
            var p = new Person("zs");
    
            // p 能否使用name属性
            console.log("name" in p);  // true
            console.log("gender" in p);  // true
            console.log("hasOwnProperty" in p);  // true
    
    
    
            // hasOwnProperty 的使用场景:
    
            // p 的原型链:
            // p ==> person.prototype  ==> Object.prototype ==> null;
    
            for(var k in p){
                // 需求: 打印的k只打印对象p自身本身的属性  --- 过滤出对象自身的属性即可
                if(p.hasOwnProperty(k)){
                    console.log(k); // true,说明k遍历所代表的属性是p对象自身的
                }
    
                // 沿着原型链遍历属性
                console.log(k); // name  gender  sex
            }
    
    
            /*小结:
             in  语法:   "属性名" in 对象
                 作用: 检测属性是否可以使用
             hasOwnProperty
                 语法:  对象.hasOwnProperty("属性名")
                 作用:检测属性是否是对象自身的
                 使用场景:  for ... in 循环中过滤出对象自身的属性*/
        </script>
    </body>
    </html>
    
  • 相关阅读:
    java 静态方法分析
    编译时常量与运行时常量
    springboot+elasticsearch配置实现
    spring+mybatise注解实现
    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
    @RequestBody 的正确使用办法
    springboot+jps+druid项目搭建
    python 源码安装
    liunx 时间ntp同步服务器
    spring 定时任务corn表达式
  • 原文地址:https://www.cnblogs.com/jianjie/p/12334602.html
Copyright © 2011-2022 走看看