Jennifer Bland header image
≡ Menu

The Difference Between .call and .apply in JavaScript

JavaScript has functions. When you execute that function then the contents of the function are performed. The easiest way to execute a function is to call it like getName().

JavaScript also provides two other methods to invoke that function:

  • getName.apply()
  • getName.call()

You might ask what is the difference between the two? Does one provide better performance than the other? Is one better to use than the other?

The best way to understand the difference between the two is to examine the pseudo syntax for each.

function.apply(thisArg, [argsArray]);
function.call(thisArg[, arg1[, arg2[, ...]]]);

Continue Reading ➞

Understanding Closure in JavaScript

A closure is a function object that retains ongoing access to the variables of the context it was created in (even after the outer function calls it was created within have returned).

In other words, the function defined in the closure ‘remembers’ the environment in which it was created.

Let me give you an example:

function getName()
  var firstName = 'Jennifer';
  function showName()
    alert(firstName);
  }
  showName();
}
getName();

Continue Reading ➞

Hack Reactor Week 3 Review

After completing my 3rd week, I realized that I am one quarter of the way through my training at Hack Reactor. Unofficially next week I will have graduated from a Freshman to a Sophomore.

Week 3 is historically considered the hardest week at Hack Reactor. After just completing that week I can agree whole heartedly with that assessment.

Monday of Week 3 started with our self-assessment. We are giving a short time period to complete an assessment that tests our knowledge of the material covered in the previous week.

Our assessment this week was to:

  • Rewrite a function declaration into pseudo-classical
  • Write a function to find duplicate characters
  • Utilized several D3 functions
  • Describe time complexity of several algorithms

Continue Reading ➞

How Hoisting in JavaScript Can Cause Unexpected Results

Your can experience unexpected results in your JavaScript programs due to execution that does not occur like you expected. A good example of this is that you can actually use a variable in JavaScript before you declare it. Here is an example:

x = "Jennifer";
for (var i = 0; i < 10; i++) {
  console.log(i);
}

var x;

JavaScript allows this code to function due to a concept known as "hoisting." Hoisting the default behavior of a JavaScript application once it executes.

Within the current scope in a JavaScript application, all variables are "hoisted" to the top regardless of where they are declared.

Continue Reading ➞

Breadth First Search in JavaScript

In my previous post I discussed the difference between a graph data structure and a tree data structure. Now that you understand the difference between the two data structures, I am going to show you how you can search through your data.

The two most common methods of searching a graph or a tree are depth first search and breadth first search.

Whether to use a depth first search or a breadth first search should be determined by the type of data that is contained in your tree or graph data structure.

Continue Reading ➞