Object.prototype.pick / Object.prototype.omit

What is the side effect? Jut due to the keyword? I have understood it, and perhaps it is not a good way to define on Object.prototype directly

in any case, please define in Object but not is Object.prototype

I get it, Object is so fundamental, thanks a lot. I will modify the specification tomorrow.

It is not proper to redefine Object.prototype because Object is especially fundamental, which may have side effects of any other objects inherited from it.

So change the proposal and define it on Object directly and update the specification at the same time:

Object.pick / Object.omit

Object.pick({a : 1, b : 2}, ['a']); // => {a : 1}
Object.omit({a : 1, b : 2}, ['b']); // => {b : 1}

Object.pick({a : 1, b : 2}, ['c']); // => {}
Object.omit({a : 1, b : 2}, ['c']); // => {a : 1, b : 2}
  1. __proto__ and prototype problem?
    hope direct throw an error.
    see Lodash search issues: __proto__
    see Lodash search issues: prototype

  2. always returns plain object?

  3. Symbol problem?
    e.q: Object.pick([], [Symbol.iterator]); // here expect is ?

const foo = Object.defineProperty({}, 'bar', {
    get() { throw new Error() }
Object.pick(foo, ['bar']) // here expect is ?

how should this error stack be designed?

  1. passthrough?
  2. wrapping original error?

If we take the implementation above, the error stack will be for the snippet:

const pick = (obj, keys) => Object.fromEntries(
    keys.map(k => obj.hasOwnProperty(k) && [k, obj[k]]).filter(x => x)

pick(Object.defineProperty({}, 'bar', {
    get() { throw new Error() }
}), ['bar']);

will be:

Uncaught Error
    at Object.get (<anonymous>:6:19)
    at <anonymous>:2:51
    at Array.map (<anonymous>)
    at pick (<anonymous>:2:10)
    at <anonymous>:5:1

But if implemented in native, the error may be able to pass through.

When it comes to the question, should the method pick up such an object, some properties of which will throw an error? I think it should not as the object can't be accessible in some way.

It depends on whether properties of prototype can be picked:

If can:

Object.pick([], [Symbol.iterator]); // => {Symbol(Symbol.iterator): f}


Object.pick([], [Symbol.iterator]); // => {}