Markovtsev Vadim
Web Programming @ MIPT, 2015-2016
This presentation is devoted to client-side Javascript (aka "in browser"); server-side (aka "general-purpose") JS is addressed in node.js.
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).
Objectprototype special attributeobj = {a: 45, "b": "test", 'c': /^abc\d+$/}
this points to function's propertiesargumentsundefined in case of empty returnthis)((msg) => console.log(msg))()
a = 10; - assign this.a to 10.
this is often window - that is, global scope.var a = 10; - declare variable a in function scope,
move the declaration to the beginning of the function; assign a
to 10.let a = 10; - declare variable a in enclosing block scope,
move the declaration to the beginning of that block; assign a
to 10.undefinedfor (var a = 0; a < 10; a++) {}console.log(a); // prints "10"
for (let a = 0; a < 10; a++) {}console.log(a); // prints "undefined"
null is very similar to undefinednull to explicitly state the object's absence, though it should existundefined in all other casesnew is used to
create new objects with constructor functionsdelete is used to
remove propertiesprototype property is used to change all objects which have been "derived"function Car() {}var car = new Car();console.log(car.color); // undefinedCar.prototype.color = null;console.log(car.color); // nullcar.color = "black";console.log(car.color); // black
class is similar to constructor function, but with fair
semantics.extends avoids the need of setting prototypeconstructor marks the initialization methodsuper gives access to inherited objectstatic makes static methods= is for assignment== (!=) is for equality checking with implicit type cast=== (!==) is for equality checking without implicit type castvar a = 4, b = '4';a == b; // truea === b; // false
if (cond) { ... } elseif (cond) { ... } - conditionalfor var|let i = 0; i < arr.length; i++) { ... } - forwhile (cond) { ... }; do { ... } while (cond) - whileswitch (expression) {} -
switch-case (=== is used for comparison)for var|let i in obj - iterates over enumerable properties
of obj in arbitrary orderfor var|let i of obj - yields values of
iterable obj, e.g.,
Arraylet 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"}
undefined.finally always have a priority over try...catchtry {throw "error";} catch (err) {return true;} finally {return false;}
function* gen() {yield 2;yield 3;yield 1;}for (let i of gen()) {console.log(i);}// 2// 3// 1
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'
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() {return2 + 2;}four(); // undefined
'' == '0' // false0 == '' // true0 == '0' // truefalse == 'false' // falsefalse == '0' // truefalse == undefined // falsefalse == null // falsenull == undefined // true" \t\r\n" == 0 // true
Javascript used to be ugly; there are special projects to improve client-side programming experience, e.g. CoffeeScript.
document and window global variables which serve the partywindow;
window.window === window is truevar heading = document.createElement("h1");heading.appendChild(document.createTextNode("Hello, World!"));document.body.appendChild(heading);
setTimeout() deferrs
function execution by the given interval in millisecssetInterval() calls
function each specified number of millisecsclearTimeout() and clearInterval() cancel execution by identifier
returned from setTimeout() and setInterval()setInterval((txt) => console.log(txt), 1000, "hello, world");
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.stringify()
performs the conversion object -> JSONJSON.parse()
restores object from JSONvar a = {field: 10}; // Object {field: 10}JSON.stringify(a); // "{"field":10}"JSON.parse(JSON.stringify(a)); // Object {field: 10}
$("#jqlist").toggle();