Let's start with a simple concept.
1 + 1
=> 2
Adding parens clarifies order of operations.
When I told people I was confused about callbacks I often heard that callbacks were just functions. So I couldn't figure out what my hurdle was to grasping them. But I just had an AH-HA! moment after getting a better grasp on some basic syntax.
JavaScript functions use ()
for two different reasons. Let me recap so that we're on the same page;
1st | Declaration
Getting comfortable with the shorthand conditional syntax. If you put the conditional on one line, the {}
are not needed.
Even when having multiple conditionals.
return
will terminate the function and spit out as it sees. No need to assign the argument to a variable.
Map returns the value of an array, as a new array.
In my quest to learn it, I went to mozialla and found the explaination unclear. I started playing around with it to learn it.
var pets = ['cat', 'dog', 'mouse', 'bird']
var more_pets = pets.map(function(value){
return value;
})
function reduce(array, start, callback) {
// javascript, and all of programming often needs variables to contain state.
var current = start,
freq = array.length;
for (var i = 0; i < freq; i++){
// how much of learning is problem solving vs. syntax?
current = callback(current, array[i]);
Notes from Eloquent JavaScript
defineProperty()
Why use Object.create verse just normal {}?
Notes from Eloquent JavaScript
Recall that the apply and bind methods both take a first argument that can be used to simulate method calls. This first argument is in fact used to give a value to this.
There is a method similar to apply, called call. It also calls the function it is a method of but takes its arguments normally, rather than as an array. Like apply and bind, call can be passed a specific this value.
speak.apply(fatRabbit, ["Burp!"]);
// โ The fat rabbit says 'Burp!'
speak.call({type: "old"}, "Oh my.");