Fullstack React Iliev 2016 1
Fullstack React Iliev 2016 1
Trayan Iliev
Agenda
1. JavaScript™ history and main features
2. JavaScript™ datatypes
3. Functions in JavaScript™
4. Closures
5. Objects in JavaScript™
6. Using this
7. Object-oriented inheritance in JavaScript™
8. JavaScript™ design patterns
9. MV* patterns for building maintainable JS UI applications
10. EcmaScript 6 (Harmony, ES 2015) novelties
11.Bootstrapping an ES6 project using Webpack
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 2
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Object-Oriented JavaScript
Object-Oriented (OO) – object literals and constructor functions
Objects can have named properites
Ex.: MyObject.name = 'Scene 1';
MyObject ['num-elements'] = 5;
MyObject.protopype.toString = function() {
return “Name: “ + this.name + “: “ + this['num-elements'] }
Configurable object properties – e.g. read only get/set etc.
Ex.: Object.defineProperty( newObject, "someKey", {
value: "fine grained control on property's behavior",
writable: true, enumerable: true, configurable: true
});
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 8
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
JavaScript Features
The state of objects could be changed using JS functions
stored in object's prototype, called methods.
Actually in JavaScript there were no real classes, - only objects
and constructor functions before ES6 (ES 2015, Harmony).
JS is dynamically typed language – new properties and
methods can be added runtime.
JS supports object inheritance using prototypes and mixins
(adding dynamically new properies and methods).
Prototypes are objects (which also can have their prototypes)
→ inhreritance = traversing prototype chain
Main resource: Introduction to OO JS YouTube video
https://www.youtube.com/watch?v=PMfcsYzj-9M
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 10
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
JavaScript Features
Datatypes in JavaScript
Primitive datatypes:
boolean – values true и false
number – floating point numbers (no real integers in JS)
string – strings (no char type –> string of 1 character)
Abstract datatypes:
Object – predefined, used as default prototype for other objects
(defines some common properties and methods for all objects:
constructor, prototype; methods: toString(), valueOf(),
hasOwnProperty(), propertyIsEnumerable(), isPrototypeOf();)
Array – array of data (really dictionary type, resizable)
Function – function or object method (defines some common
properties: length, arguments, caller, callee, prototype)
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 12
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Datatypes in JavaScript
Special datatypes:
null – special values of object type that does not point
anywhere
undefined – a value of variable or argument that have not
been initialized
NaN – Not-a-Number – when the arithmetic operation should
return numeric value, but result is not valid number
Infinity – special numeric value designating infinity ∞
Operator typeOf
Example: typeOf myObject.toString //-->'function'
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 13
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Functional JavaScript
Functional language – functions are “first class citizens”
Functions can have own properties and methods, can be
assigned to variables, pass as arguments and returned as a
result of other function's execution.
Can be called by reference using operator ().
Functions can have embedded inner functions at arbitrary depth
All arguments and variables of outer function are accessible to
inner functions – even after call of outer function completes
Outer function = enclosing context (Scope) for inner functions →
Closure
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 14
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Closures
Example:
function countWithClosure() {
var count = 0;
return function() {
return count ++;
}
}
var count = countWithClosure(); <-- Function call – returns innner
function wich keeps reference to
count variable from the outer scope
console.log( count() ); <-- Prints 0;
console.log( count() ); <-- Prints 1;
console.log( count() ); <-- Prints 2;
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 15
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Functions in JavaScript
Embedded functions – define their own scope scope - Ex.:
function getBoundingRectangle(pts) {
var points = pts || [];
Local variables
function minX() {
var x, min = Number.POSITIVE_INFINITY;
for(var i = 0; i < points.length; i++){ ...
x = points[i].x; return {
if( x < min){
x: minX(),
min = x;
Object literal y: minY(),
}
width: maxX() - minX(),
}
height: maxY() - minY()
return min;
}
}
}
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 17
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Functions in JavaScript
Inner functions define their own scopes:
function getBoundingRectangle(pts) {
var points = pts || []; Functional literal
var minX = function () {
var x, min = Number.POSITIVE_INFINITY;
for(i = 0; i < points.length; i++){
x = points[i].x;
if( x < min){ return {
min = x; x: minX,
} y: minY,
} width: function() {return maxX() - minX();},
return min; height: function() {return maxY() - minY();}
} }}
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 18
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
ECMAScript 5:
var self = this;
this.nums.forEach(function (v) {
if (v % 5 === 0)
self.fives.push(v);
});
ES6 Promises
})
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
// someApp.js
import * as math from "lib/math"
console.log("2π = " + math.sum(math.pi, math.pi))
// otherApp.js
import { sum, pi } from "lib/math"
console.log("2π = " + sum(pi, pi))
TypeScript
[http://www.typescriptlang.org/]
Typescript → since October 2012, Anders Hejlsberg (lead
architect of C# and creator of Delphi and Turbo Pascal)
Targets large scale client-side and mobile applications by
compile time type checking + @Decorators -> Microsoft, Google
TypeScript is strictly superset of JavaScript, so any JS is valid TS
EcmaScript 6 Compatibility
[http://kangax.github.io/compat-table/es6/]
MVC
MVVM
MVP
Sources:https://en.wikipedia.org/wiki/Model_View_ViewModel#/media/File:MVVMPattern.png,
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
https://en.wikipedia.org/wiki/Model%E2%80%93view
International License Slide 40
%E2%80%93presenter#/media/File:Model_View_Presenter_GUI_Design_Pattern.png
License: CC BY-SA 3.0, Authors:Ugaya40, Daniel.Cardenas
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Resources
Crockford, D., JavaScript: The Good Parts. O'Reilly, 2008.
Douglas Crockford: JavaScript: The Good Parts video at
YouTube – http://www.youtube.com/watch?v=_DKkVvOt6dk
Douglas Crockford: JavaScript: The Good Parts presentation at
http://crockford.com/onjs/2.pptx
Koss, M., Object Oriented Programming in JavaScript –
http://mckoss.com/jscript/object.htm
Osmani, A., Essential JavaScript Design Patterns for Beginners
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
Fielding's REST blog – http://roy.gbiv.com/untangled/2008/rest-
apis-must-be-hypertext-driven
Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
International License Slide 45
Full-stack Development with IPT – Intellectual Products & Technologies
Node.js and React.js Trayan Iliev, http://www.iproduct.org/
Questions?