Table of Contents
IntroductionFrontend Masters intro Douglas Crockford introduces himself, gives a little introduction, and goes over the agenda for the course.
Part 1: Programming Style & Your Brain (00:01:54-01:15:03)
The Way We ThinkOur process of thought stems from two separate systems: System 1: Our Gut and System 2: Our Head. Visual processing is something human brains do better than the fastest super-computers. However visual processing can create conflicts between our two different systems. An illusion is an example of what is seen by System 1 (our gut) but may not make sense logically to system two (our head). Advertisers have always attempted to sell products to our Gut not to our Head. They try to appeal to us visually, not logically.
Avoiding Confusing Code (A)Confusing code should be avoided. Use programming forms that avoid adding confusion. Looking at block scope versus function scope reveal common pitfalls with variable scope.
Avoiding Confusing Code (B)Additional pitfalls emerge from the use of global variables and syntax shortcuts. Bad coding styles fall into four classes: Under Educated, Old School, Thrill Seekers, and Exhibitionist Programming is the most complicated thing that humans do. Computer programs must be perfect. Humans are not good at perfection so designing a good programming style demands discipline.
Using JSLintThe JSLint style was driven by the need to automatically detect defects. Not all features of a language should be used just because they are available. The key is finding a reliable subset within the language. Performance-specific code can be confusing. Only optimize the code that is taking the time. Algorithm replacement is vastly more effective than code fiddling. Bugs will exist no matter what coding style you use. All programmers can do is limit the chance for bugs. Part 1 wrap up and questions
NumbersExploring the Number type and using the Math object. NaN can be confusing, but ultimately it’s the result of undefined or erroneous operation.
ObjectsArrays and Object are often interchangeable. It’s best to use Objects when the names are arbitrary strings. Arrays are often more efficient when the names are sequential numbers. All values are objects except for null and undefined. The type of prefix operator will return a string identifying the type of a value. Use Array.isArray for Array validation.
Part 3: Function the Ultimate (02:24:01-03:09:01)
Functions as SubroutinesThe idea of a subroutine is the origin of functions. Subroutines allow for code reuse, modularity and recursion. With closures, the context of an inner function includes the scope of the outer function. An inner function continues to have access to that context even after the parent functions have returned. Function scope works like block scope.
Prototypal Inheritance (B)Using functions as modules may be the best way to create inheritance. This module pattern can be easily transformed into a powerful constructor pattern using functional inheritance. Part 3 Wrap-up and questions.
Part 4: Problems (03:09:02-03:48:54)
Problems 1-5A sequence of problems will be presented followed by a solution. Each problem builds on the last so if you get a problem wrong, use the solution to begin the next problem. First, a quick quiz: What is x? o Problem 1: Write a function that takes an argument and returns that argument. o Problem 2: Write two binary functions, add and mul, that take two numbers and return their sum and product. o Problem 3: Write a function that takes an argument and returns a function that returns that argument. o Problem 4: Write a function that adds from two invocations. o Problem 5: Write a function that takes a binary function, and makes it callable with two invocations.
Problems 6-9o Problem 6: Write a function that takes a function and an argument, and returns a function that can supply a second argument. o Problem 7: Without writing any new functions, show three ways to create the inc function. o Problem 8: Write methodize, a function that converts a binary function to a method. o Problem 9: Write demethodize, a function that converts a method to a binary function.
Problems 10-12o Problem 10: Write a function twice that takes a binary function and returns a unary function that passes its argument to the binary function twice. o Problem 11: Write a function compseu that takes two unary functions and returns a unary function that calls both of them. o Problem 12: Write a function compseb that takes two binary functions and returns a function that calls both of them.
Problems 13-15o Problem 13: Write a function that allows another function to only be called once. o Problem 14: Write a factory function that returns two functions that implement an up/down counter. o Problem 15: Make a revocable function that takes a nice function, and returns a revoke function that denies access to the nice function, and an invoke function that can invoke the nice function until it’s revoked.
Part 5: Monads & Gonads (03:48:55-04:50:36)
Introduction to MonadsMonads are a powerful functional pattern. There are two ways to use functional programming. One ways is by simply programming with function. The other way is much more mathematical. In the real world, everything changes. Immutability makes it hard to interact with the world. Monads are a loophole in the function contract.
The Ajax MonadThe Ajax monad is another example of a monad design pattern. We can add the ability to bind additional methods to our monad to create Ajax functionality.
The Promise Monad (A)Promises are an excellent mechanism for managing asynchronicity. A promise is an object that represents a possible future value. An example of using promises can be found with the Filesystem API.
The Promise Monad (B)Using the composition pattern, a promise can be created by nesting when statements. The result will look very familiar: A promise is a monad. There are a number of additional resources and videos regarding monads and monad-related concepts.
Audience Questionso What is your preferred text editor? o What is your favorite notation? Additional audience questions o Why is the prototyping approach in object-oriented programming more powerful than a classes-based approach?