-1

const obj = {
    foo: 1,
    bar: 2
};

let fn = (...obj) => {
    console.log(foo, bar); // ReferenceError: foo is not defined
};

fn(obj);

I basically want to use the object props directly inside the function without having to obj.foo or obj.bar.

Edited to add: I understand I can define the args manually ({ foo, bar }), but I don't want to do that since the object is expected to have unknown / variable number of properties.

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • There's no way to populate the lexical environment with the keys of an object. Closest thing is `with` but it's not recommended at all. – MinusFour Aug 01 '19 at 23:20
  • Do you just want to iterate its props to the console? – Icepickle Aug 01 '19 at 23:20
  • I think the edit kinda screwed up the question, it doesn't really make any sense anymore, it really doesn't state one bit what you want out of this. It seems like asking if you could do it lazier than it already is? What is the idea afterwards? do you want to create a new object based on the rest of the props and the named props? – Icepickle Aug 01 '19 at 23:27
  • @3zzy If you don't know what the property names are, how are you going to refer to them in the function? – Daedalus Aug 01 '19 at 23:29
  • @Daedalus I know the required ones, but there might be additional one depending on certain conditions. – eozzy Aug 01 '19 at 23:30
  • @3zzy Question still stands, since you are basing your requirement on accessing those additional unknowns. – Daedalus Aug 01 '19 at 23:31
  • I've not seen that syntax. As far as I know, you are not destructuring anything. You have a spread operator as an argument. Looks like they need to update their error handling. – StackSlave Aug 01 '19 at 23:40

2 Answers2

1

Destructuring for objects differs than array:

const obj = {
    foo: 1,
    bar: 2
};

let fn = ({ foo, bar }) => {
    console.log(foo, bar); // ReferenceError: foo is not defined
};

fn(obj);

You could also use destructuring on values if you just want to loop over properties:

const obj = {
    foo: 1,
    bar: 2
};

let fn = (values) => {
    console.log(...values); // ReferenceError: foo is not defined
};

fn(Object.values(obj));
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • Yea, I kind of knew this, but the thing is, the object can have any number of props so don't want to define manually. – eozzy Aug 01 '19 at 23:16
  • arf, I guessed you would have these kinds of expectations... However, I don't think there is such a feature in javascript. Apart from using a `for..of` loop on your object... – Ulysse BN Aug 01 '19 at 23:18
  • 1
    @3zzy can also do `let fn = ({ foo, bar, ...rest })` and all other properties are in `rest` – charlietfl Aug 01 '19 at 23:19
1

Does this help?

I assume you are after some properties that you do know about. But want to retain the others ?

const obj = {
    foo: 1,
    bar: 2,
    unknown: 3,
    unknow: 4,
};

let fn = ({ foo, bar, ...other}) => {
    console.log(foo, bar); 
    console.log(other);
};

fn(obj);
Bibberty
  • 4,670
  • 2
  • 8
  • 23
  • I honestly don't see the advantage of that approach, unless you want to return a "clone" with some specified properties, but then again, the question (after the edit) became quite unclear – Icepickle Aug 01 '19 at 23:24