So this is a thing in JS:

var state = 'tom';
var o = {
  name: 'test',
  state: state,
  ...( ( true && ( this.state = 'jamie' ) ) && false ) && { state }

Figure out what it does.

  • 2
    `( ( true && ( this.state = 'jamie' ) ) && false ) && { state }`
    this part of the code does literally nothing, other than set this.state to 'jamie'
  • 0

    You'd think so right? But you'd be wrong.
  • 4
    Blaming a language for a dev abusing its features
  • 0

    Not blaming it at all. Just confused by it is all.
  • 2
    And this, children, is why you don't use var and why you prefer const (although in this case const doesn't help much, but use it anyway!)
  • 3
    @inaba and its mutable then use let
  • 0
    Rainbows maybe I dunno
  • 0
  • 1
    V8 tells you to screw off and quits.
  • 1
    I don't know what the ... Does. But the rest of it is simple.
  • 1
    As said earlier, OP is using the language incorrectly. But to be fair I didn't even know the spread operator was a thing.
  • 0
    Here it is running
  • 0
    Running pt 2
  • 1
    Let me get in my computer to explain what's going on here because typing code into a phone is fucked
  • 0

    It isn't incorrect, it's just not how it should be used. There's nothing stopping it being used that way other than the fact it's horrible to read.
  • 2
    Okay so what's going on here is really simple:

    We're defining a global variable, state, and assigning "tom" to it. We are then creating an object called o, and giving it some useless data (name), and it's own variable called "state", which we set to the global variable "state", which is "tom".

    Then, we use the spread operator. This is a new one, refer to the screenshots above for what it does. Let's break down the obfuscated code.

    ( ( true && ( this.state = 'jamie' ) ) && false ) && { state }

    the hardest part here is ( this.state = 'jamie' ). See the screenshot attached: "this" refers to Window (the global scope), and since assignment returns what you assigned, this just evaluates to 'jamie'

    ( ( true && 'jamie' ) && false ) && { state }

    The rest is easy to figure out knowing how booleans work in JS. Anything other than 0 is true. so this is:

    ((true && true) && false) && true

    (true && false) && true

    false && true


    You're spreading false into an object (continued...)
  • 3
    ...and spreading false does nothing because it's a single value. Spreading any single value, surprisingly, does nothing whatsoever to an object.

    Therefore, what this code does, is define a global variable, make an object with a useless variable, a variable that copies the global variable, and then a spread which evaluates code which re-assigns the global variable and does not affect the object itself.

    Thanks for coming to my ted talk.
  • 1

    "It isn't incorrect, it's just not how it should be used."

    Just. Just please. Stop and think about what you have said here.

    I can go south down a north highway. When the cop stops me asks me why I'm driving the wrong way on the road, I'll say

    "Oh, it's not wrong. It's just not the way it was intended. There's nothing stopping me from doing it this way except it looks a little funny"

    And then I go to jail and lose my license for life.
Your Job Suck?
Get a Better Job
Add Comment