其他分享
首页 > 其他分享> > Object.keys——Object.getOwnPropertyNames——对象.hasOwnProperty

Object.keys——Object.getOwnPropertyNames——对象.hasOwnProperty

作者:互联网

1.

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;
    //     }

}
Object.assign(Point.prototype, {
    fun1() {}
});
Point.prototype.fun2 = function() {}

var p = new Point(1,2);
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
p.age = 100
console.log(p.age)
//使用set age(x){}才能取到值

 

标签:console,log,Point,keys,age,Object,hasOwnProperty,getOwnPropertyNames
来源: https://www.cnblogs.com/sunupo/p/15539267.html