r/javascript • • Mar 09 '15

Things every JavaScript developer should know

http://ilikekillnerds.com/2015/03/things-every-javascript-developer-should-know/
326 Upvotes

118 comments sorted by

View all comments

28

u/cgaudreau senior HTML9 engineer Mar 09 '15 edited Mar 09 '15

Good article. However, correct me if I'm wrong, but the provided FizzBuzz implementation appears to be incorrect. On multiples of 15, the provided solution would print "FizzBuzz Fizz Buzz".

EDIT: the author fixed it after I posted. The original example had no elses or else ifs.

0

u/[deleted] Mar 09 '15

[deleted]

9

u/[deleted] Mar 09 '15

The good takeaways are:

  1. Your browser has an element selector engine built in

  2. JS has a few different types of loops

  3. There is more than console.log() available to you

  4. Use strict, and what it does

  5. Call and apply, use them

1

u/Sinistralis Mar 11 '15

I honestly don't see the value in Call or Apply when you can just use objects to pass data around between functions.

I know it has use with currying if you are going the functional programming route, but beyond that I have yet to run into a case where I needed to utilize Call or Apply. If you have some examples, I would love to see them.

2

u/[deleted] Mar 11 '15

Forcing context on callbacks mostly

2

u/Sinistralis Mar 11 '15

Can I get an actual code example, if you don't mind?

3

u/[deleted] Mar 11 '15 edited Mar 11 '15

I am not good at explaining but you should read

http://javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/

and

http://javascript.info/tutorial/binding

or

http://blog.bigbinary.com/2011/08/18/understanding-bind-and-bindall-in-backbone.html

But basically:

What is the difference between call and apply anyway? They both call the function?

The main difference between call and apply is that apply accepts an array of arguments.

Should you have a function like: function mixColor(color1, color2)

You can call it like mixColor.apply(undefined, colors)

      mixColor.call(undefined, colors[0], colors[1])

While still allowing for mixColor(red, green) instead of mixColor([red, green])

There are also situations you might use it for variable length arguments, etc.

But that was not my real question, when the heck do you want to apply scope, why not pass in data???

Imagine this situation:

     var person = {
              name
              dateOfBirth
              function notifyBBQReady() ...
              function BBQReady() ...
      }

and you have a function which calls:

     somelibrary.cookburgers().done(person.BBQReady)

when changeName is called from the promise, it will not have the "this" argument being person. If you use data like you suggested when I ask for "this.name" or call this.notifyBBQReady() it does not work as the "this" is not pointed at the person. If I passed "person" in as data I have to always use "person.name" and "person.notifyBBQReady()" and I can never call "person.BBQReady()" from within the object without using it differently from a regular method on the object.

So you can do something like:

     somelibrary.cookburgers().done(function() {person.BBQReady.apply(this, arguments)})

and the proper scope will be contained.

You have also likely seen it used in examples like this:

      ParentClass.prototype.somemethod.apply(this, arguments)

This is how folks say to call the parent class method, etc.

In short, imagine this:

         function dosomething(callback) {
                        ....
                       callback()
         }

         quitButton {  function successstatus() { this.$el.css('color', 'green'); } }
       

         dosomething(quitButton .successstatus)

Will not work correctly but:

        dosomething(function() { quitbutton.successstatus.call(quitbutton) }) will

and

      dosomething(quitbutton.successstatus.bind(quitbutton)) also will 

In shorter:

In javascript, any time you call a method on an object without the actual object as the context you get into trouble..but javascript being used as an OO language you often want a proper "this" instead of passing data around to every call you make.

1

u/Sinistralis Mar 11 '15

Ah, it's all about using the "this" context. I avoid situations that involve using "this" entirely by using classless objects that are built by being passed through interfaces and built in layers, so that each object inherently knows what to do when invoked, despite possibly being completely seperate objects.

I appreciate this explanation though, it may come in handy one day if we are ever handed code with this style in place.

5

u/kenavr Mar 09 '15

I never heard of it. Maybe because I am not from the US or because I never actually interviewed for a developer position, but I don't understand how this example can be challenging to someone who wants to work in this field.

1

u/neckro23 Mar 10 '15

That's the entire point. It's a test of baseline competence.

0

u/[deleted] Mar 09 '15

[deleted]

4

u/IwishIwasAnAllBlack Mar 10 '15

Probably a blessing in disguise... If they feel that knowing what a fizzbuzz is, is THAT important you probably dont wanna work for them anyway

0

u/[deleted] Mar 09 '15

[deleted]

0

u/[deleted] Mar 09 '15

[deleted]

2

u/aufdemwegzumhorizont Mar 09 '15

what?

3

u/od_9 Mar 09 '15

Parent is technically correct in that it saves a mod operation, but it also can save up to 2 comparisons (and associated jumps) as well, that's the real optimization. Using the other method requires exactly 3 comparisons every loop, but the posters method requires at most 3. This is also the definition of overthinking things.

3

u/Racoonie Mar 10 '15

But it's still an incredibly shitty implementation.

   var result = '';
    if (!(i % 3)) result += 'Fizz';
    if (!(i % 5)) result += 'Buzz';

2

u/ike_the_strangetamer Mar 09 '15

the example uses a chain of 'else if' and 'else' after the first conditional, so those won't be evaluated if the first condition is true.

3

u/cgaudreau senior HTML9 engineer Mar 09 '15

Indeed. Looks like the author fixed it. It originally used ifs exclusively.

2

u/[deleted] Mar 09 '15

It was changed

-2

u/zarl0ck Mar 09 '15 edited Mar 10 '15

No, it would print FizzBuzz only and then it would cycle to the next for loop. For example if i = 15, it checks the first condition i % 15 (true) and prints FizzBuzz. It's not going to enter inside the other if conditions because it already entered in the first one.

EDIT: so the author changed the code, it was wrong when he posted the comment and it was right when I posted mine.. got mixed up, no need for downvotes