What do square brackets around an expression mean, e.g. `var x = a + [b]`?

后端 未结 7 1803
清酒与你
清酒与你 2020-12-04 12:00

We have received some JavaScript from an agency that looks wrong, but works.

For some reason they are adding square brackets ([, ]) around v

相关标签:
7条回答
  • 2020-12-04 12:49

    It's possible to construct a situation where this:

    var some_variable = 'to=' + other_variable;
    

    and this:

    var some_variable = 'to=' + [other_variable];
    

    produce different results. Specifically, if the Array.prototype.toString() method (or, I suppose, the Array.prototype.join() method) has been changed from its default, anything could happen. For example, extra functionality could be added to the Array.prototype.toString() method to generate logging information, do some lookups, or anything really.

    The likelihood that this has been done is slim, I'd imagine, but it needs to be mentioned for completeness.

    0 讨论(0)
提交回复
热议问题