Web Programming @ MIPT, 2015-2016

Markovtsev Vadim

ES6 (Javascript)

Web Programming @ MIPT, 2015-2016

What is Javascript

Annotation

This presentation is devoted to client-side Javascript (aka "in browser"); server-side (aka "general-purpose") JS is addressed in node.js.

History

Engines

Modern JS engines use just-in-time compilation to achieve better performance.

There is asm.js subset of JS, normally generated by special tools, which becomes very close to ideal (about 2x-1.5x slowdown).

Objects

obj = {a: 45, "b": "test", 'c': /^abc\d+$/}
		

Functions

Arrow functions

((msg) => console.log(msg))()
		

Semicolons

Variables

for (var a = 0; a < 10; a++) {}
console.log(a);  // prints "10"
			
for (let a = 0; a < 10; a++) {}
console.log(a);  // prints "undefined"
			

null vs undefined

new and delete

function Car() {}
var car = new Car();
console.log(car.color);  // undefined
Car.prototype.color = null;
console.log(car.color);  // null
car.color = "black";
console.log(car.color);  // black
		

Classes

getters and setters

var obj = {
  get foo() { return 10; }
};
obj.foo;  // 10

=, == and ===

var a = 4, b = '4';
a == b;  // true
a === b;  // false

Primitives

Loops

let arr = [3, 5, 7];
arr.foo = "hello";

for (let i in arr) { console.log(i); // logs "0", "1", "2", "foo" }
for (let i of arr) { console.log(i); // logs "3", "5", "7" }

Exceptions

try {
  throw "error";
} catch (err) {
  return true;
} finally {
  return false;
}
		

Generators

Reference
function* gen() {
  yield 2;
  yield 3;
  yield 1;
}
for (let i of gen()) {
  console.log(i);
}

// 2 // 3 // 1

eval and inspection

eval() executes arbitrary JS code.

eval('console.log("test");');
		

Object.keys(obj) returns all the properties of obj

f.toString() returns function f's source code'

Modes

function foo() {
  "use strict";
}

WTF?!

Javascript can be awkward. There is even a dedicated collection of it's dark sides: wtfjs. Some examples taken from StackOverflow:

'5' + 3  // '53'
'5' - 3  // 2
'5'- - '3'  // 8
function four() {
  return
    2 + 2;
}
four();  // undefined
''        ==   '0'           // false
0         ==   ''            // true
0         ==   '0'           // true
false     ==   'false'       // false
false     ==   '0'           // true
false     ==   undefined     // false
false     ==   null          // false
null      ==   undefined     // true
" \t\r\n" ==   0             // true
					

CoffeeScript

Javascript used to be ugly; there are special projects to improve client-side programming experience, e.g. CoffeeScript.

Minification

API: DOM manipulation

Example of DOM manipulation:
var heading = document.createElement("h1");
heading.appendChild(document.createTextNode("Hello, World!"));
document.body.appendChild(heading);

API: deferred execution

setInterval((txt) => console.log(txt), 1000, "hello, world");
		

API: JSON

There are cases when it is needed to convert an object into some text/binary representation which can be stored on disk or sent over the internet. This convertion is called serialization.

  • JSON is a versatile open format ideal for the web
  • JSON.stringify() performs the conversion object -> JSON
  • JSON.parse() restores object from JSON
var a = {field: 10};  // Object {field: 10}
JSON.stringify(a);  // "{"field":10}"
JSON.parse(JSON.stringify(a));  // Object {field: 10}

		

jQuery

$("#jqlist").toggle();

d3.js

Next steps

View on Github