The purpose of this presentation is also largely to explain some of the fun parts of JavaScript (dynamic function invocation with reflection, dynamic arguments, etc). I don't necessarily encourage putting complex logic throughout your templates with lodash, though there may be times it is appropriate.
Lets talk about lodash and how it can easily be married to Angular. Using a six line filter, I will expose the power of lodash directly within angular templates. This will be an introduction to lodash, angular templates, and we'll talk about scope and reflection within JavaScript.
14. JavaScript does not have block scope;JavaScript does not have block scope;
it has lexical scope.it has lexical scope.
var something = 1;
{
var something = 2;
}
console.log(something);
-> 2
15. var something = 1;
function run() {
var something = 2;
console.log(something);
}
run();
console.log(something);
-> 2
-> 1
16. var something = 1;
function run() {
if (!something) {
var something = 2;
}
console.log(something);
}
run();
-> 2
JavaScript Variable HoistingJavaScript Variable Hoisting
18. Higher-order FunctionsHigher-order Functions
"Functions that operate on other"Functions that operate on other
functions, either by taking them asfunctions, either by taking them as
arguments or by returning them"arguments or by returning them"
http://eloquentjavascript.net/05_higher_order.htmlhttp://eloquentjavascript.net/05_higher_order.html
19. function greaterThan(n) {
return function(m) { return m > n; };
}
var greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// -> true
Higher-order FunctionsHigher-order Functions
notenote nn is still available withinis still available within
the returned functionthe returned function
20. ClosuresClosures
"A closure is a special kind of object that"A closure is a special kind of object that
combines two things: a function, andcombines two things: a function, and
the environment in which that functionthe environment in which that function
was created."was created."
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closureshttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
21. function makeFunc() {
var name = "Pangolin";
function displayName() {
debugger;
alert(name);
}
return displayName;
};
var myFunc = makeFunc();
myFunc();
ClosuresClosures
28. JavaScript ContextJavaScript Context
The environment in which a functionThe environment in which a function
executes.executes.
thethe thisthis keywordkeyword
Context is most often determined byContext is most often determined by
how a function is invokedhow a function is invoked
29. Function Statement ContextFunction Statement Context
function billMe() {
console.log(this);
function sendPayment() {
console.log(this);
}
sendPayment();
}
billMe();
The context for forThe context for for
function statement isfunction statement is
the global objectthe global object
34. jQueryjQuery
$( "li" ).each(function myIterator(index) {
$( this ).text();
});
jQuery controls the context of the callbackjQuery controls the context of the callback
andand thisthis becomes eachbecomes each lili elementelement
35. AngularAngular
angular.module('app')
.controller('Customers', function() {
var vm = this;
vm.title = 'Customers';
});
Angular controls the context of the controller.Angular controls the context of the controller.
WithWith controllerAscontrollerAs the contextthe context becomesbecomes
bound to the template.bound to the template.