zoukankan      html  css  js  c++  java
  • Object.keys——Object.getOwnPropertyNames——对象.hasOwnProperty

    1.

     


    // console.log(Object.keys(p.prototype)) // 报错
    console.log(Object.keys(p))
    //['x', 'y']
    console.log(Object.keys(Point.prototype))
    //['fun1', 'fun2']
    console.log(Object.getOwnPropertyNames(Point.prototype))
    //['constructor', 'add', 'age', 'fun1', 'fun2']
    console.log(Object.getOwnPropertyNames(p))
    //['x', 'y']
    console.log(Object.getOwnPropertyNames(new Point(3,4)))
    //['x', 'y']
    console.log(p.hasOwnProperty("x"))
    //true
    console.log(p.hasOwnProperty("y"))
    //true
    console.log(p.hasOwnProperty("z"))
    //false

    class Point {
        constructor(x, y) {
            this.x = x;
            this.y = y;
        }
        add(a, b) {
            return a + b
        }
        get age() {
            return this._age;
        }
        //     set age(x){
        //       this._age = x;
        //     }
    
    }
    var z = 4;
    Object.assign(Point.prototype, {
        fun1() {},
        z
    
    });
    Point.prototype.fun2 = function() {}
    
    var p = new Point(1,2);

      //console.log(Object.getOwnPropertyNames()) //必须传参

    // console.log(Object.keys(p.prototype)) // 报错
    console.log(Object.keys(p))
    //['x', 'y']
    console.log(Object.keys(Point.prototype))
    //['fun1', 'fun2']
    console.log(Object.getOwnPropertyNames(Point.prototype))
    //['constructor', 'add', 'age', 'fun1', 'fun2']
    console.log(Object.getOwnPropertyNames(p))
    //['x', 'y']
    console.log(Object.getOwnPropertyNames(new Point(3,4)))
    //['x', 'y']
    console.log(p.hasOwnProperty("x"))
    //true
    console.log(p.hasOwnProperty("y"))
    //true
    console.log(p.hasOwnProperty("z"))
    //false
    
    p.age = 100
    console.log(p.age)
    //使用set age(x){}才能取到值
    console.log(p.z)
    //4
  • 相关阅读:
    Luogu P5030 长脖子鹿放置(网络流)
    BZOJ3037 创世纪(基环树DP)
    LuoguP1240 诸侯安置
    LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)
    总结-一本通提高篇&算竞进阶记录
    LuoguP5022 旅行 (割点,基环树)
    $tsinsenA1067$
    $SCOJ4427 Miss Zhao's Graph$
    $Edmonds-Karp$[网络流]
    $AC自动机$
  • 原文地址:https://www.cnblogs.com/sunupo/p/15539267.html
Copyright © 2011-2022 走看看