inline destructuring through exclude (or some other syntax)

One of the benefits of destructuring is that it makes the syntax for passing parameters more convenient. To that end i would like to propose the following

instead of

let {  name, year, } = obj
 doRest( rest)

which is nice but it would be even nicer if one could just do this

doRest(obj.exclude({name,year}) )

Related: Object.prototype.pick / Object.prototype.omit - #32 by aleen42

If that gets through, then you could do the following:

doRest(Object.omit(obj, ['name', 'year']))

i like omit because it is shorter but {} makes it clearer what is being omitted and no quotes is less typing

This new syntax also has an issue where it already means something else in javascript.

Code such as this:


could already exist, for example:

const obj = {
  exclude(data) { ... }

let name = 'x'
let year = 3
// This calls obj.exclude() with the object { name: name, year: year }

I would suggest considering Object.prototype closed for any string-named additions, ever.

You claim has been true for any extension to object from the very beginning of the definition of object (and array as well) . I would prefer to add more features to object than add functions like Set or Map that add more complexity to the language. Adding more functions in effect has the same issue you point to for the object prototype but for the language itself because it requires adding more reserved words.

Are you saying that we should close down the language to new reserved words?

Or it this an Object specific complaint because ? (not sure what the logic is that distinguishes Object from other items)

It's specific to Object.prototype, which isn't just inherited by normal objects, but also by functions, as well as every primitive in the language (true.exclude() doesn't really make sense), and Sets, Maps, RegExps, Dates, etc. Additionally, it's not inherited from by null objects - ie, Object.create(null), or the ns in import * as ns, so a prototype method doesn't help you for "all objects" anyways.

A static method on Object is the only thing that would make sense for something like this, if it were API (as opposed to syntax).

1 Like

I guess, I'm a little confused if this is supposed to be entirely syntax, or if "exclude" is an actual function on every object, that when it gets called, allows for special syntax inside the parentheses, or something.

i.e. would this be possible, like it is with other functions?

const obj = { a: 1, b: 2 }
const myExclude = obj.exclude.bind(obj)
myExclude({ a }) // Does this log { b: 2 } ??

Maybe it would help if you enlighten us on what would happen in scenarios like this, where "exclude" is already a member of the object. What would all of these log out?

const obj = { a: 1, b: 2, exclude: x => x }
let a = 5

console.log(obj.exclude({ a })) // Does this log out { b: 2, exclude: x => x }, or will this call the exclude function on obj with the object { a: 5 }?
console.log(obj.exclude) // Invalid syntax? Or does this give back the exclude function on obj?
console.log(obj['exclude']) // Does this log out the exclude function on obj?
console.log(obj.exclude(2)) // Invalid syntax? Or does this call the exclude function on obj?

excellent point.

I was thinking about how works only with object. But it seems that the only way to do this is to define something like Object.keys or object.prototype.omit, both of which are a bit ham fisted.

So if i had to choose Object.omit (obj, {key1,key2}) would be the least painful to write and understand.

This really begs the issue of a new syntax for special object types.Right now there is [ ] and "" as well as alpha+| numeric for naming keys. I wonder if something extra would work such as

obj..omit or obj!omit or some other way to do it. There might be some typescript issues but it seems that typescript is the new dos and we should all be more careful about adopting it at our collective peril anyway.