• Building universal Windows 10 applications • Possible candidate for cross-platform mobile development • Cordova / Phonegap, Ionic • Also server side development (Backend) • Replacing / Complementing XML for data transfer (REST + JSON)
mobile app • Better deployment and & maintanence • Mobile users need to get access to everything Image: http://coenraets.org/blog/wp-content/uploads/2011/10/directory11.png
single web page • Fluid UX, like desktop app • Examples like Gmail, Google maps • Html page contains mini-views (HTML Fragments) that can be loaded in the background • No reloading of the page, better UX • Requires handling of browser history, navigation and bookmarks
one way of communicating between computers over the internet • Data can be send via XML or JSON using HTTP • HTTP GET • http://something/employee/1 • Result • {id: 1, name: "jack"} • If both frontend and backend is JS, object sending and parsing extremely easy
runtime environment • Uses V8 (Chrome) JS engine • Additional modules on top of JS to provide functionality • Web server, system i/o, networking, ... • Lot of third party modules available
the World Wide Web • Web Standards consist of the following • Recommendations of the World Wide Web Consortium (W3C) • (X)HTML, CSS, SVG, DOM ... • RFC documents published by Internet Engineering Task Force (IETF) • URI, HTTP, MIME ... • And also standards by Ecma International (JavaScript), Unicode (Charsets) and IANA (Name and number registries).
(W3C) is the main international standards organization for the World Wide Web • Compatibility among industry members • W3C makes recommendations for the web • Several recommendations • XHTML, DOM, CSS, XML... • URL: http://www.w3.org/
• Originally JavaScript, then LiveScript and then back to JavaScript. • JScript • Microsoft made their own version of the JavaScript • Lead to compatibility problems • => ECMAScript, effort to standardize different versions of the J(ava)Script
International • In Browsers, ECMAScript is commonly called JavaScript • JavaScript = Native (EcmaScript) + Host objects (browser) • Java/ECMAscript is nowdays heavily used with AJAX – based sites • Still many problems because of browser incompatibilites
EcmaScript developed by Microsoft • It has types! • When EcmaScript 6th Edition was working progress, typescript gave already classes, modules, arrow function syntax. • Now that the 6th Ed. is ready the additional benefit using TypeScript is types (and others like interfaces, generics...) • TypeScript is compiled to EcmaScript using compiler • It's first class language in Microsoft Visual Studio and in frameworks like Angular 2.0
console.log(variable); > tsc typescript.ts > cat typescript.js var variable = "typescript"; console.log(variable); Notice that compiler compiles typescript to javascript and it loses the string type because that is not part of js!
string = "typescript"; variable = 8; console.log(variable); > tsc typescript.ts typescript.ts(2,1): error TS2322: Type 'number' is not assignable to type 'string'.
International • ECMAScript is commonly called JavaScript when having host objects • JavaScript = Native (EcmaScript) + Host objects • Host objects can be objects from browser environment or for example from NodeJS
primitive datatypes • Boolean (true, false) • Null (value that isn’t anything) • Undefined (undefined value) • Number - floating point number (64 bit) • String (16 bit UNICODE) • Symbol (ECMAScript 6) • And Objects (all the rest)
null is converted to 0 "5" + null // returns "5null" because null is converted to "null" "5" + 1 // returns "51" because 1 is converted to "1" "5" - 1 // returns 4 because "5" is converted to 5
= Number("12"); • parseInt(value[, radix]), converts start of the string • var i = parseInt("12px”, 10); • Radix? • 10 => integer number, 8 => octal number, 16 => hexadecimal • NaN (Not a Number), check using isNaN(variable) • Result of erroneous operations
Date('December 17, 1995 03:24:00'); var birthday = new Date('1995-12-17T03:24:00'); var birthday = new Date(1995, 11, 17); var birthday = new Date(1995, 11, 17, 3, 24, 0); var unixTimestamp = Date.now(); // in milliseconds
combinations in strings • In JS regex is an object • var re = new RegExp("ab+c"); • For better performance • var re = /java/; • Example var regex = new RegExp("java"); if("javascript".match(regex)) { console.log("Match found!"); }
they have more methods and auto-increment key-values • var stuff = ["a", "b", "c"] • The keys are now 0, 1 and 2 • The length of the array is 3 and it's linked to numerical properties
value pairs (properties) • var car = { brand: "Ford", year: 2015 } • Possible to create properties dynamic • var car = new Object(); car.brand = "Ford"; • Possible to use also bracket notation • car["year"] = 2015 • Delete key-value pair • delete car["year"]
simple key-value pairs var sayings = new Map(); sayings.set("dog", "woof"); sayings.set("cat", "meow"); for (var [key, value] of sayings) { console.log(key + " goes " + value); } • Map object advantages over Objects: • Map keys can be anything, in objects it's string • Map has size information • Iteration in maps are the insert order
values var mySet = new Set(); mySet.add(1); mySet.add("some text"); mySet.add("foo"); mySet.has(1); // true mySet.delete("foo"); mySet.size; // 2 for (var item of mySet) console.log(item); • Set object advantages over arrays: • Only unique values • Checking element if exists is faster • Can delete values, in array's its splice
HTML Forms or window.prompt("", ""); • Output: DOM manipulation, document.write("") or window.alert(""); • In V8/NodeJS or Rhino/Spidermonkey • Output to CLI: print(".."); • Input from CLI: Is not that easy... • Debugging • console.log("");
Functions are objects! It's possible to pass them as variables • If function is object's property, it's a method • Functions uses variable hoisting, let's see this later
} console.log( add(1,1) ); // THIS IS THE SAME THAN ABOVE! // Creating a function object var adder = new Function('a', 'b', 'return a + b'); console.log( adder(4,4) ); // Because it's an object you can pass these! var x = adder; console.log( x(9,9) );
debugging var somethingFoo = function somethingBar() { print("do something"); // Recursion, does not end well here. somethingBar(); } somethingFoo(); // this does not work somethingBar();
For more professional approach, let's dive into cli and create our apps without the browser • Very useful / efficient when creating large scale JS apps
js apps in command line. • Several JS engines • SpiderMonkey (c++) or Rhino (java) (Firefox) • SquirrelFish (Apple’s Webkit) • V8 (Google Chrome) • It’s possible to install these as command line apps, although the process can be little difficult
Including functions and arrays • Object contains properties and methods • Collection of name-value pairs • Names are strings, values can be anything • Properties and methods can be added at runtime • Objects can inherit other objects
object var obj = new Object(); obj.x = 10; obj.y = 12; • This adds dynamically two properties to the obj – object! • Object is built – in data type • Objects are key value pairs!
to Person (for example global object) if(!(this instanceof Person)) { // Let's then use the new word return new Person(name); } else { this.name = name; } } // Now both work var vilma = new Person("Vilma"); var jaska = Person(”Jaska");
Douglas Crockford • http://jslint.com • Inspects and warns about potential problems • “Will hurt your feelings” • Excepts that your code is in “strict mode” • Can be used via website or command line (installation required) • Command line tool (Java wrapper for JSLint) • http://code.google.com/p/jslint4java/
maintaining compatible with EcmaScript 5 • Introduces Strict mode • Removes features from the language! Raises errors that were okay in non strict mode • Backward compatible • Add “use strict”, in function or global scope • EcmaScript supports non-strict mode, but it’s depricated!
statement prevented • Assignment to non-declared variables prevented (i = 3) • Eval is prohibited • Lot of other issues.. • See ES5 specification page 235