EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

keys #62

Open EdwardZZZ opened 5 years ago

EdwardZZZ commented 5 years ago
const color = (str, color) => process.stdout.isTTY ? `\x1B[${color}m${str}\x1B[0m` : str;

class Test {
    constructor() {
        this[Symbol('a')] = '';
        this.z = '1';
    }
    [Symbol('b')]() {}
    a() {}
    b() {}
}

let obj = Test.prototype;
console.log('>>1');
console.log(Object.keys(obj));
console.log(Reflect.ownKeys(obj));
console.log(Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj));

obj = Object.getOwnPropertyDescriptors(Test.prototype);
console.log('>>2', JSON.stringify(obj));
console.log(Object.keys(obj));
console.log(Reflect.ownKeys(obj));
console.log(Object.getOwnPropertyNames(obj));

obj = new Test();
console.log('>>3');
console.log(Object.keys(obj));
console.log(Reflect.ownKeys(obj));
console.log(Object.getOwnPropertyNames(obj));

// obj = Object.getOwnPropertyNames(obj);
// obj = Object.getOwnPropertySymbols(obj);
obj = Reflect.getPrototypeOf(obj);
console.log('>>4', obj);
console.log(Object.keys(obj));
console.log(Reflect.ownKeys(obj));
console.log(Object.getOwnPropertyNames(obj));

obj = {
    [Symbol('c')]() {},
    a() {},
    b() {},
}
console.log('>>5');
console.log(Object.keys(obj));
console.log(Reflect.ownKeys(obj));
console.log(Object.getOwnPropertyNames(obj));

const obj2 = Object.create(obj);
obj2.n = 3;
console.log('k' in obj2);
console.log(Reflect.has(obj2, 'k'));
console.log(Object.hasOwnProperty(obj2, 'k'));