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.
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.
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.
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.
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.
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.
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.
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
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.