乐趣区

js以变量调用json未知keys的方法

运算符:
let firstProperty = {

foo: 'abc',
bar: 123

}::getPropertyByIndex(0);

function getPropertyByIndex(index) {

let keys = Object.keys(this);
let key = keys[index];
return this[key];

}

或者 getter:
Object.defineProperty(Object.prototype, ‘properties’, {

get() {
    return Object
        .keys(this)
        .map(key => this[key]);
}

});

https://www.jianshu.com/p/575…

退出移动版