r/jquery • • Oct 16 '15

Well this is some handsome code.

http://imgur.com/QtIPDne
39 Upvotes

40 comments sorted by

1

u/ReFokus Oct 16 '15

Define: Handsome code

2

u/jasokant Oct 17 '15

...did you just ask yourself to define handsome code? :p

1

u/ReFokus Oct 17 '15

No, I asked you guys.

1

u/ReFokus Oct 17 '15

Does anyone know why my code doesn't work when I don't write the document ready? Anyone having similar problems?

0

u/[deleted] Oct 17 '15

[deleted]

1

u/ReFokus Oct 17 '15

2.1.3

0

u/[deleted] Oct 17 '15

[deleted]

1

u/ReFokus Oct 17 '15

It's any jQuery code. Without $(document).ready(); it doesn't work, no errors, nothing. It just doesn't execute.

5

u/ForScale Oct 16 '15

I don't get it...

0

u/[deleted] Oct 16 '15

[deleted]

2

u/ForScale Oct 16 '15

Tahuhwhatnow?

0

u/[deleted] Oct 16 '15

[deleted]

2

u/ForScale Oct 16 '15

WARNING: Infinite loop found in previous comment.

4

u/dumsumguy Oct 16 '15

[SERIOUS] What do we think is the cleanest way to write a chain like this?

0

u/[deleted] Oct 16 '15

[deleted]

3

u/fooey Oct 17 '15 edited Oct 18 '15

I'm always a fan of using Async.js for things like this instead of building my own, but I'm probably a bit of a library addict.

http://codepen.io/jasonr/pen/BomwYY

const queue = [
    {el: '#blue', action: 'slideDown'}, 
    {el: '#red', action: 'slideDown'}, 
    {el: '#green', action: 'slideDown'}, 
    {el: '#black', action: 'slideDown'}, 
    {el: '#black', action: 'slideUp'}, 
    {el: '#green', action: 'slideUp'}, 
    {el: '#red', action: 'slideUp'}, 
    {el: '#blue', action: 'slideUp'}, 
    {el: '#dog', action: 'slideDown'},
];


$(document).ready(function() {
    $('.divs').show().hide();

    async.eachSeries( 
        queue,
        (item, cb) =>
        $(item.el)[item.action](cb)
    );
});

1

u/Coffee2Code Oct 17 '15

And now in vanilla JavaScript :D

0

u/ndboost Oct 18 '15

this is vanilla js..

1

u/Coffee2Code Oct 18 '15

No... It's jQuery...

0

u/[deleted] Oct 17 '15

[deleted]

1

u/Coffee2Code Oct 17 '15

It was kind of a dare, but, why not make a website where people show their way and people can vote on it?

I can do the hosting and backend stuff, and I would want to use Symfony as the framework..

0

u/[deleted] Oct 17 '15

[deleted]

1

u/Coffee2Code Oct 17 '15

We can always give it a spin and see if it flies.

1

u/pirso Oct 16 '15
 var item = arr.shift();

Clever!

1

u/cresquin Oct 17 '15

cases like this are why it's called .shift()

0

u/[deleted] Oct 17 '15

[deleted]

3

u/SuperFLEB Oct 17 '15
while (wall_beers.length) { passAround(wall_beers.shift()); }

?

2

u/rodneon Oct 17 '15

Maybe, but it doesn't make semantic sense for wall_beers to be an array. I would imagine the pseudo-code to be more like

while (wall_beers) { passAround(--wall_beers); }

3

u/SuperFLEB Oct 17 '15

I suppose that depends on whether every wall_beer is the same or not, or if they need to retain properties like state.

0

u/cresquin Oct 17 '15

slightly more compact

$(document).ready(function() {
  $(".divs").hide();
  var arr = [
    {
      el: "#blue",
      direction: "Down"
    },
    {
      el: "#red",
      direction: "Down"
    },
    {
      el: "#green",
      direction: "Down"
    },
    {
      el: "#black",
      direction: "Down"
    },
    {
      el: "#black",
      direction: "Up"
    },
    {
      el: "#green",
      direction: "Up"
    },
    {
      el: "#red",
      direction: "Up"
    },
    {
      el: "#blue",
      direction: "Up"
    },
    {
      el: "#dog",
      direction: "Down"
    }
  ];

  function slide() {
    var item;
    if(typeof (item = arr.shift()) !== "undefined") {
      $(item.el)["slide"+item.direction](slide);
    } else {
      alert('so derp');
    }
  }
  slide();

});

3

u/rodneon Oct 17 '15

Very clever, but I'm having a hard time understanding this line:

$(item.el)"slide"+item.direction;

I get that it turns into $("#red")"slideDown". First of all, I didn't know you could call functions that way, which could make for really interesting code. But how does (slide) fit in? Why is the function being passed to the slideDown call...

...never mind, I think I just answered my own question. You're passing the slide function as a callback.

This is a brilliant solution. Thanks for sharing.

0

u/cresquin Oct 17 '15

The bracket notation is used to reference a member of an object. Effectively obj.prop is the same as obj["prop"]. The difference is that using dot notation you can only reference predetermined members of an object, and those members must be named using only unreserved characters. Using bracket notation, though, you can dynamically build a string to reference a member. For instance:

var a = "foo",
    b = "bar",
    o = { foobar: 10 };
o[a + b] === 10;

2

u/rodneon Oct 17 '15

That much I knew. It just never occurred to me that I could call functions the same way. It was my first time seeing a function being called this way:

ObjectName['FunctionName'](arguments)

0

u/[deleted] Oct 17 '15

[deleted]

1

u/[deleted] Oct 17 '15

Even better: Do a var makeslide = function (arr) { return function () { ... }; } where the inner function is the old slide.

Or, if you can, use currying.

2

u/SuperFLEB Oct 17 '15

All the repetition reminds me of fade-in effects from back in the day, where you'd make a whole bunch of BODY statements to fade in the color...

<BODY BGCOLOR="#000000">
<BODY BGCOLOR="#010101">
<BODY BGCOLOR="#020202">
<BODY BGCOLOR="#030303">
<BODY BGCOLOR="#040404">
<BODY BGCOLOR="#050505">
<BODY BGCOLOR="#060606">

...etc.

2

u/bishopZ Oct 17 '15

callback hell

3

u/myevillaugh Oct 16 '15

You're being sarcastic, right?

0

u/uberpwnzorz Oct 16 '15

This is why promises were created.

0

u/[deleted] Oct 16 '15

[deleted]

3

u/baabaa_blacksheep Oct 16 '15

Have yet to fiddle with promises. Any nice starting point that you know of?

2

u/ForScale Oct 16 '15

Expound please... I struggle with the concept of promises...

0

u/[deleted] Oct 16 '15

[deleted]

3

u/jpolito Oct 16 '15

I really like that analogy! Wonderful explanation

2

u/ForScale Oct 16 '15

That does help a bit. Thanks!!

0

u/[deleted] Oct 16 '15

[deleted]

3

u/SuperFLEB Oct 17 '15

Well, at least it's (mostly) single-threaded, so your race conditions don't involve multiple simultaneous actions.