JavaScript FundamentalsJavaScript Fundamentals
with Angular and Lodashwith Angular and Lodash
Bret Little - @little_bret
blittle.github.io/blog/
http://slides.com/bretlittle/js-fundamentals-angular-lodash
JavaScript scope is not $scopeJavaScript scope is not $scope
Just because you can, doesn't mean you should
Caution!Caution!
<div
class='active-users'
ng-repeat='user in users | lodash:"filter":{active: true}'>
{{ user.name }}
</div>
var users = [
{ 'name': 'barney', 'age': 36, 'active': true },
{ 'name': 'fred', 'age': 40, 'active': false }
]
_.filter(users, { 'active': true })
// returns [{ 'name': 'barney', 'age': 36, 'active': true }]
<div class='selected-user'>
{{ users
| lodash:'findWhere':{active: true, age: 36}
| lodash:'result':'name' }}
</div>
var users = [
{ 'name': 'barney', 'age': 36, 'active': true },
{ 'name': 'fred', 'age': 40, 'active': false }
]
_.result(
_.findWhere(users, { 'active': true, 'age': 36 }), 'name'
)
// returns 'barney'
<div ng-repeat="i in 10 | lodash:'range'">
{{ $index }}
</div>
_.range(10);
// → [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<span>{{name | lodash:'capitalize'}}</span>
$scope.name = 'alfred';
<span>Alfred</span>
<span class='{{dynamicClassName | lodash:'kebabCase'}}'>
Hello
</span>
$scope.dynamicClass = 'someDyanmicClassName';
<span class='some-dyanmic-class-name'>Hello</span>
<span>
{{value | lodash:'padLeft':10:0}}
</span>
$scope.value = 123;
<span>0000000123</span>
<span>
{{longVal | lodash:'trunc':28}}
</span>
$scope.longVal = 'Crocodiles have the most acidic stomach
of any vertebrate. They can easily digest bones, hooves
and horns.';
<span>Crocodiles have the most ...</span>
<div ng-repeat='user in users
| lodash
:"slice":(page * amountPerPage)
:((page + 1) * amountPerPage)'
>
{{user.name}}
</div>
<button ng-click='page = page - 1'>Previous</button>
<button ng-click='page = page + 1'>Next</button>
http://output.jsbin.com/zesuhuhttp://output.jsbin.com/zesuhu
angular.module('myApp')
.filter('lodash', function() {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
JavaScript FundamentalsJavaScript Fundamentals
Higher-order FunctionsHigher-order Functions
ClosuresClosures
Scope & ContextScope & Context
Dynamic function invocationDynamic function invocation
ArgumentsArguments
JavaScript 2015JavaScript 2015
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
var something = 1;
function run() {
var something = 2;
console.log(something);
}
run();
console.log(something);
-> 2
-> 1
var something = 1;
function run() {
if (!something) {
var something = 2;
}
console.log(something);
}
run();
-> 2
JavaScript Variable HoistingJavaScript Variable Hoisting
angular.module('myApp')
.filter('lodash', function() {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
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
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
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
function makeFunc() {
var name = "Pangolin";
function displayName() {
debugger;
alert(name);
}
return displayName;
};
var myFunc = makeFunc();
myFunc();
ClosuresClosures
var counter = (function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
},
decrement: function() {
changeBy(-1);
},
value: function() {
return privateCounter;
}
};
})();
console.log(counter.value()); // logs 0
counter.increment();
console.log(counter.value()); // logs 1
Practical ClosuresPractical Closures
angular.module('myApp')
.filter('lodash', function(someService) {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
angular.module('myApp')
.filter('lodash', function() {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
function sayHello() {
for (var i = 0, iLength = arguments.length; i < iLength; i++) {
console.log('Hello', arguments[i]);
}
}
sayHello('Chester Nimitz', 'Raymond Spruance');
Dynamic ArgumentsDynamic Arguments
-> Hello Chester Nimitz
-> Hello Raymond Spruance
angular.module('myApp')
.filter('lodash', function() {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
angular.module('myApp')
.filter('lodash', function() {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
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
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
Object ContextObject Context
var obj = {
foo: function(){
return this;
}
};
obj.foo();
obj.foo() === obj; // true
Constructor ContextConstructor Context
function Legate(rank) {
this.rank = rank;
}
var legate = new Legate(100);
console.log(legate.rank);
Dynamic Function ContextDynamic Function Context
function add(c, d){
return this.a + this.b + c + d;
}
var o = {a:1, b:3};
add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16
add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34
Function.prototype.bindFunction.prototype.bind
var myWidget = {
type: 'myAwesomeWidget',
clickCallback: function() {
console.log(this.type);
}
}
document.getElementById('submit').addEventListener(
'click', myWidget.clickCallback.bind(myWidget)
);
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
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.
angular.module('myApp')
.filter('lodash', function() {
return function(input, method) {
var args = [input].concat(
Array.prototype.slice.call(arguments, 2)
);
return _[method].apply(_, args);
}
});
import _ from 'lodash';
import angular from 'angular';
angular.module('app')
.filter('lodash', function() {
return (input, method, ...args) => (
_[method].apply(_, [input, ...args])
)
});
1. http://ryanmorr.com/understanding-scope-and-context-in-javascript/
2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
4. http://eloquentjavascript.net
5. JS 2015-2016 - https://babeljs.io/
6. Axel Rauschmayer - http://www.2ality.com/
ResourcesResources
JavaScript Fundamentals with Angular and Lodash

More Related Content

PDF
Lodash js
PDF
Chaining and function composition with lodash / underscore
PPTX
TDD in the wild
PDF
Variables, expressions, standard types
ODP
Scala introduction
PDF
Intro programacion funcional
PDF
Selectors and normalizing state shape
KEY
Into Clojure
Lodash js
Chaining and function composition with lodash / underscore
TDD in the wild
Variables, expressions, standard types
Scala introduction
Intro programacion funcional
Selectors and normalizing state shape
Into Clojure

What's hot (20)

PPTX
JQuery Presentation
PPTX
Ian 20150116 java script oop
DOCX
What are arrays in java script
PDF
JS OO and Closures
PPTX
ActionScript3 collection query API proposal
PPTX
Drupal7 dbtng
PPTX
Drupal 8 database api
PPTX
Js types
KEY
MTDDC 2010.2.5 Tokyo - Brand new API
PDF
Codeware
PPTX
Prototype Framework
PPTX
Javascript And J Query
PPT
JavaScript Arrays
PPTX
Java script arrays
PDF
Magic methods
PDF
Functional Core, Reactive Shell
PDF
Swift 함수 커링 사용하기
PPTX
jQuery
PDF
Python 3.x Dictionaries and Sets Cheatsheet
PDF
Java script objects 1
 
JQuery Presentation
Ian 20150116 java script oop
What are arrays in java script
JS OO and Closures
ActionScript3 collection query API proposal
Drupal7 dbtng
Drupal 8 database api
Js types
MTDDC 2010.2.5 Tokyo - Brand new API
Codeware
Prototype Framework
Javascript And J Query
JavaScript Arrays
Java script arrays
Magic methods
Functional Core, Reactive Shell
Swift 함수 커링 사용하기
jQuery
Python 3.x Dictionaries and Sets Cheatsheet
Java script objects 1
 
Ad

Similar to JavaScript Fundamentals with Angular and Lodash (20)

PDF
Advanced javascript
PDF
Rails is not just Ruby
PDF
Introduction to Protractor
KEY
JavaScript Growing Up
PPTX
LinkedIn TBC JavaScript 100: Functions
PDF
Bonnes pratiques de développement avec Node js
PDF
Ten useful JavaScript tips & best practices
PPT
Ruby on Rails Intro
PPT
Introduction to Javascript
PDF
Javascript Frameworks for Joomla
PDF
What's new in jQuery 1.5
PDF
Avinash Kundaliya: Javascript and WordPress
PDF
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
PDF
international PHP2011_Bastian Feder_jQuery's Secrets
PDF
Construire une application JavaFX 8 avec gradle
PPTX
ES6 Overview
KEY
The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
PDF
jQuery and Rails: Best Friends Forever
PDF
Struts2 notes
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Advanced javascript
Rails is not just Ruby
Introduction to Protractor
JavaScript Growing Up
LinkedIn TBC JavaScript 100: Functions
Bonnes pratiques de développement avec Node js
Ten useful JavaScript tips & best practices
Ruby on Rails Intro
Introduction to Javascript
Javascript Frameworks for Joomla
What's new in jQuery 1.5
Avinash Kundaliya: Javascript and WordPress
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
international PHP2011_Bastian Feder_jQuery's Secrets
Construire une application JavaFX 8 avec gradle
ES6 Overview
The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
jQuery and Rails: Best Friends Forever
Struts2 notes
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Ad

Recently uploaded (20)

PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Java Basics-Introduction and program control
PPTX
Petroleum Refining & Petrochemicals.pptx
PPTX
wireless networks, mobile computing.pptx
PDF
Unit1 - AIML Chapter 1 concept and ethics
PDF
First part_B-Image Processing - 1 of 2).pdf
PPTX
ai_satellite_crop_management_20250815030350.pptx
PPTX
Feature types and data preprocessing steps
PPTX
CyberSecurity Mobile and Wireless Devices
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PPTX
Principal presentation for NAAC (1).pptx
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PDF
Design of Material Handling Equipment Lecture Note
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PPTX
Amdahl’s law is explained in the above power point presentations
DOC
T Pandian CV Madurai pandi kokkaf illaya
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PDF
Applications of Equal_Area_Criterion.pdf
PPTX
PRASUNET_20240614003_231416_0000[1].pptx
PPTX
Module 8- Technological and Communication Skills.pptx
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Java Basics-Introduction and program control
Petroleum Refining & Petrochemicals.pptx
wireless networks, mobile computing.pptx
Unit1 - AIML Chapter 1 concept and ethics
First part_B-Image Processing - 1 of 2).pdf
ai_satellite_crop_management_20250815030350.pptx
Feature types and data preprocessing steps
CyberSecurity Mobile and Wireless Devices
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Principal presentation for NAAC (1).pptx
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Design of Material Handling Equipment Lecture Note
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Amdahl’s law is explained in the above power point presentations
T Pandian CV Madurai pandi kokkaf illaya
MLpara ingenieira CIVIL, meca Y AMBIENTAL
Applications of Equal_Area_Criterion.pdf
PRASUNET_20240614003_231416_0000[1].pptx
Module 8- Technological and Communication Skills.pptx

JavaScript Fundamentals with Angular and Lodash