how in angular *.ts files i can call somethins like this:
const a = {a: 1};
a.get('a'); // return 1
trying to do something like below, but always error in compiler or console
Object.defineProperties(Object.prototype, {
get: {
value: (key: string, defaultValue = null) => {
console.log(this);
if (!this || !key) {
return defaultValue;
}
const keyArr = key.split('.');
const itemKey = keyArr.shift();
let result = this[ itemKey ];
if (result === undefined) {
if (this instanceof Array && isNaN(+itemKey)) {
const resultArr = Array.from(this.keys())
.map((k: any) => {
keyArr.unshift(k, itemKey);
return this.get(keyArr.join('.'));
})
.filter(res => res !== null);
return resultArr.length ? resultArr : defaultValue;
}
result = defaultValue;
}
return result && keyArr.length ? this.get(keyArr.join('.'), defaultValue) : result;
},
enumerable: false,
configurable: false,
writable: true
}
});
errors:
ERROR in src/app/app.component.ts(15,24): error TS2339: Property 'get' does not exist on type 'Object'.
Invalid property descriptor. Cannot both specify accessors and a value or writable attribute