Javascript - Crockford On JS Act III
Just some gleaned insights taken from Douglas Crockford's Act III presentation on Javascript. You'll likely want to watch the video to understand why these things are important.
- Declare all variables at the top of a function.
- Declare all functions before you call them.
- Treat arguments as read-only.
thisis available to an inner function through assigning the outerthisto another variable.applyandcallcan specify whatthisrefers to.function=>thisis global object orundefined, method =>thisis the object calling the method, constructor =>thisis the new object, apply/call =>thisis passed in as an argument.- Promise making is queuing up calls to happen when something happens that is likely asynchronous.
- Don't make functions in a loop.
- Y Combinator - mind is now blown.
"[Closure] is one of the most important features in javascript."
"[Closure] is the thing that makes javascript one of the world's brilliant programming languages."
Y Combinator
function y(le) {
return (function (f) {
return f(f);
}(function (f) {
return le(function (x) {
return f(f)(x);
});
}));
}
var factorial = y(function (fac) {
return function (n) {
return n <= 2 ? n : n * fac(n - 1);
};
});
var number120 = factorial(5);
