1

I have the following code in a Javascript

function(proposal){
  var items = (this.editor, []);
  proposals || (proposals = []), items.push(....

I would guess items is declared as array. But than why do I have the brackets? When I try it on the regular console than I would get items=[]. And what is the benefit to include this.editor? Hmmmm... is there anything declared additionally?

And the sense of the second line? I agree that if proposal is undefined than it would be []. But this makes for me only sense in the context of a variable declaration. Since it is in an own line does this statement make any kind of sense?

LeO
  • 4,238
  • 4
  • 48
  • 88
  • 1
    It would appear that the `this.editor,` part is entirely superfluous. Unless it has some side effect (which would be unexpected and therefore terrible code), this does nothing. – deceze Feb 25 '21 at 09:52
  • *"I would guess items is declared as array."* It's *declared* as a *variable*. It's *initialized* with an array. *"But than why do I have the brackets?"* There's no good reason unless either A) You want an error if `this` is `null` or `undefined`, or B) `this.editor` is an accessor property and it has a side-effect that you want to occur. *"I agree that if proposal is `undefined` than it would be `[]`"* Not just `undefined`, *any* falsy value, including `0`or `""`. ... – T.J. Crowder Feb 25 '21 at 09:55
  • *"But this makes for me only sense in the context of a variable declaration."* No, it can be done anywhere, it's just an expression. Here it's being used to set a default value for `proposal` if `proposal` is any falsy value. (In modern JavaScript you'd use an actual default parameter value instead, which has the advantage of only taking effect if the parameter value received is, specifically, `undefined`.) So on the whole, I wouldn't take any lessons from this code you've found except as examples of things either it's pointless to do or that have a more modern replacement. – T.J. Crowder Feb 25 '21 at 09:57
  • 1
    @T.J.Crowder: Thx for explantion. Since I need to work on this code I wanted to have some kind of understanding. Your response gives me some hints which I highly appreciate. – LeO Feb 25 '21 at 10:09

0 Answers0