Function binding

  • setTimeout is a window object method so ‘this’ will always refer to window by default.  (#2)
  • To override ‘this’ default binding, pass in an anonymous function (#1) or use .bind (#5) or .call (.call will invoke the function immediately).
  • setTimeout will automatically invoke the passed in function after wait time.
    • If you want to call the function immediately, use .call (#3) or invoke the function when you pass it in as argument (#4).
  • Good refresher on the differences between call, bind, and apply.
    • Call invokes the function and allows you to pass in arguments one by one.
    • Apply invokes the function and allows you to pass in arguments as an array.
    • Bind returns a new function, allowing you to pass in a this array and any number of arguments.
var name = "Window";
var alice = {
  name: "Alice",
  sayHi: function() {
    alert(this.name + " says hi");
  }
};
var bob = { name: "Bob" };
setTimeout(function() {
  alice.sayHi();
}, 1000);
// #1. alice says hi, after 1 second. 

setTimeout(alice.sayHi, 1000); 
// #2. window says hi, after 1 second.

setTimeout(alice.sayHi(), 1000); 
// #3. window says hi, immediately.

setTimeout(alice.sayHi.call(bob), 1000);
// #4. bob says hi, immediately

setTimeout(alice.sayHi.bind(bob), 1000);
// #5. bob says hi, after 1 second.

Note to self:

When you bind outside setTimeout, you have to save it to a variable in order to keep it bound.  Below is an example of a failed binding of the sayHi function to bob:

var name = "Window";
var alice = {
  name: "Alice",
  sayHi: function() {
    alert(this.name + " says hi");
  }
};
var bob = { name: "Bob" };


alice.sayHi.bind(bob); // this needs to be saved!!

setTimeout(alice.sayHi(), 1000);
// alice says hi, immediately.
Advertisements
Function binding

JavaScript Functions

Javascript: Object-oriented programing language that adds interactivity to websites.  Allows less server interaction as you can validate user input before sending it off to the server — this leads to lower server load & traffic.

Higher-order functions:  A function that takes other function(s) as arguments, or returns a function as its result.

Callback function:  A function that is passed into another function as an argument.  It’s being “called back” by the higher-order function.

Closure:  global variables can be made local or private using closures.  A closure gives you access to an outer function’s scope from an inner function.

 

JavaScript Functions

Unit Testing

Unit testing is when you write a test for each unit (or each function/method) in a program. Each unit test is made up of assertion statements.

assertEqual: This can only be used to check scalar values (numbers or strings), not objects or arrays.

Screen Shot 2017-03-15 at 11.44.50 AM

assertWithinRange: This checks if output is within a certain range.

Screen Shot 2017-03-15 at 11.46.12 AM

assertArraysEqual:  To assert that two arrays are equal, you must first check that they’re both equal in values and lengths.

Screen Shot 2017-03-15 at 11.41.14 AM

assertObjectsEqual: objects must be converted to strings first to be compared.

Screen Shot 2017-03-15 at 11.45.51 AM

How to call an assertion:

assertEqual(findLongestPalindrome('racecar hannah'), 'racecar', 'finds longest of two palindromes');
Unit Testing

Some Array Methods

var newArray= Array.from(oldArray);

var newArray = oldArray.slice(); 

Makes a new copy of the old array instead of just referencing it.


Array.isArray(input); 

Checks if a value is an array.


arr.slice(begin, end);

Extracts a portion of an array based on provided indices. (Omitting begin will default it to index 0.  Omitting end will default to extracting up to the end arr.length).


arr.splice(startIndex, deleteCount, ‘item1‘, item2‘, etc.);

Remove or insert element where start is.  (Set deleteCount to 0 if you’re inserting).

Some Array Methods

Big O

Big O notation describes the performance or complexity of an algorithm.

Screen Shot 2017-03-09 at 12.12.04 PM

O(1) – Constant time complexity – describes an algorithm (a one-line statement code) that will always execute in the same time (or space) regardless of the size of the input data set. An example is accessing a value of an array.

var arr = [ 1,2,3,4,5];
arr[2]; // => 3

 

O(N) – Linear time complexity – describes an algorithm (usually a loop) whose performance will grow linearly and in direct proportion to the size of the input data set. For example, if the array has 10 items, we have to print 10 times. If it has 1,000 items, we have to print 1,000 times.

//if we used for loop to print out the values of the arrays
for (var i = 0; i < array.length; i++) {
  console.log(array[i]);
}

 

O(log N) – Logarithmic time complexity – describes an algorithm where have a large set of data and you halve the dataset at each iteration until you get the result that you’re looking for.  An example of this is finding a word in a dictionary (binary search).   Sorting a deck of cards (merge sort) would be O(N log N).

Other examples:

Example 1:
for (var i = 1; i < n; i = i * 2)
  console.log(i);
}
Example 2:
for (i = n; i >= 1; i = i/2)
 console.log(i);
}

 

O(N2) – Quadratic time complexity – represents an algorithm whose performance is directly proportional to the square of the size of the input data set. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N3), O(N4) etc.  Examples include checking for duplicates in a deck of cards, bubble sort, selection sort, or insertion sort.

for(var i = 0; i < length; i++) {     // has O(n) time complexity
    for(var j = 0; j < length; j++) { // has O(n^2) time complexity
      // More loops?
    }
}

 

O(2N) – Exponential time complexity – denotes an algorithm whose growth doubles with each addition to the input data set. An example of an O(2N) function is the recursive calculation of Fibonacci numbers.  Another example is trying to break a password by testing every possible combination (assuming numerical password of length N).

Amortized time: 

If you do an operation say a million times, you don’t really care about the worst-case or the best-case of that operation – what you care about is how much time is taken in total when you repeat the operation a million times.

So it doesn’t matter if the operation is very slow once in a while, as long as “once in a while” is rare enough for the slowness to be diluted away (that the cost is “amortized”).  Essentially amortised time means “average time taken per operation, if you do many operations“.

Source: stackoverflow

Space complexity: 

Sometimes we want to optimize for using less memory instead of (or in addition to) using less time. Talking about memory cost (or “space complexity”) is very similar to talking about time cost and we also use the Big O notation.

Big O Cheatsheet.

Big O

OO Programming vs Functional Programming

In all programs, there are two primary components: the data (the stuff a program knows) and the behaviors (the stuff a program can do to/with that data).

Object-Oriented Programming: programming paradigm that brings together data and its associated behaviors (functions) in a single location called an “object”.

  • data structure is usually an array of objects.  (Data is converted into objects and methods are called on them).

Example:

class Employee
  def initialize(name, salary)
    @name = name
    @salary = salary
  end

  def change_salary(amt)
    @salary = @salary + amt
  end

  def description
    "#{@name} makes #{@salary}"
  end
end
employees = [
  Employee.new("Bob", 100000.0),
  Employee.new("Jane", 125000.0)
]

employees.each do |emp|
  emp.change_salary(10000.0)
end

employees.each do |emp|
  puts emp.description
end

Functional Programming: programming paradigm that says data and behavior are distinctively different things and should be kept separate for clarity.

  • data structure is an array of arrays and/or hashes to not *complicate* data by mixing it with behavior.
  • FP leans very heavily on tiny methods that do one small part of a larger job, delegating the details to other tiny methods. This combining of small methods into a larger task is call “composition”.
  • This idea of not changing the contents (or “state”) of a variable once it’s been created is called immutability and is another key aspect of FP.

Example:

employees = [
  [ "Bob",  100000.0 ],
  [ "Jane", 125000.0 ]
]
happier_employees = change_salaries(employees, 10000.0)
happier_employees.each do |emp|
  puts "#{emp[0]} makes #{emp[1]}"
end
OO Programming vs Functional Programming