On Github mawek / mawek.github.io
Marek Gerhart @ GoodData 2013
Rozmysla som ako ponat tuto prezentaciu. nechcel som aby to bol len pouhy vycet vlastnosti alebo referencna prirucka. Ale ono sa to moc neda pri prezentacii tohto typu Tak som sa tu snazil zhrnut len tie zakladne rysy jazyka, ktore ho odlisuju od ostatnych jazykov, pripadne nejake veci ktore nie su az take intuitivne a zrejme na prvy pohlad.
Javascript is not just for moving html elements around the page anymore
Ked som este robil v et netere, maintainoval som jeden eshop v ich CMS a obcas tam trebalo urobit aj nejaku upravu na UI - vacisnou to teda robili supportaci ale niekedy som sa tomu nevyhol dni ja.
Nenavidel som to - povazoval som to za manualnu pracu na ktorej nie je nic tvorive, proste trebalo prida par html elementov niekde, nejake dalsie css classy a obcas este niecim zahybat cez jQuery a nieco poanimovat. Trebalo to furt ladit pre rozne prehliadace a bola to taka spinava praca. Snazil som sa tomu vyhybat.
Aby bolo jasne - o html a css som vedel nutne minimum, vzdy som si vygooglil aky selektor mam pouzit a ake atributy do css vlastne patria.. Co sa tyka js tak to som nepoznal skoro vobec, skopiroval som co som nasiel na nete a ono to nejako fungovalo. Isiel som na to skor intuitivne.
Web is the future - everything is moving to web
V poslednom case ale zacal webovy svet zazivat revoluciu - zacalo sa pomerne dost rozpravat o html5, css3 - selektor sem selektor tam, rozne css transformacie, javascriptove kniznice, bootstrap, knockout, backbone, node, angular.. nemal som tucha ako tie veci funguju Islo to mimo mna ale nejako som registroval ze je okolo toho rozruch. Bolo toho pomerne dost a uvedomoval som si ze sa to uz neda ignorovat, ze to je pomerne velka vec.
Preto ked prisla konzola - CCC tak som si povedal ze to je skvela prilezitost ako sa do toho ponorit, ako sa naucit nieco nove - co je lepsie ako cisto novy projekt, pre 1-2 ludi..
Tak som do toho skocil ale bolo toho vela. Vobec som nevedel co kde je ale bol som hodeny rovno do vody - javascript, ember, handlebars, sass, mocha, karma, grunt, node,...
Don't think about javascript as of language - think about it as of web platform
Javascript ecosystem is rich and it's on rise
Javascriptovy svet je teraz na vzostupe, je tam ked bola java pred 10 rokmi. Js vyzera ako sracka jazyk ale nedajte sa oklamat, java tiez nie je ziadne terno. A najlepsie na jave nie je jazyk, ale JVM a cely ekosystem co okolo toho existuje. A presne to plati aj pre javascript - je to najrozsirenejsi jazyk, da sa s tym robit hafo veci, teraz to prenika aj do backendu a do toolingu a nejde to ignorovat
JS is interpreted. But some browsers do optimize code - i.e. chrome compiles js to native code
can be omited between lines and js will insert it itself
var a; a = 5 console.log(a) // the same as var a; a=5; console.log(a) var a=b+c (d+g).toString() // js will search for function c(dg)
function swap1(a,b){ sum = a + b; var sum; // this will get hoisted return sum; } function swap2(a,b){ sum = a + b; var sum = 0; // assignment doesn't get hoisted return sum; // return 0 always }
everything that is not a primitive type is an object
var a = 5; a.toString(); /* define property on wrapper object * but it will be disposed after this statement */ a.someProperty = 'someValue'; console.log(a); // undefined
typeof null === 'object' // WTF?
var obj = {x: 5, y: 3}; obj.x === 5; obj.y === 3; delete obj.x; // return true obj.x === undefined; // x doesn't exist on obj anymore obj.y = undefined; obj.y === undefined; // y property is still there!
var fun1 = function(){alert('hello world');} function namedFunction(){alert('hello world');}
Let's do it java style: 0 - based months, 1 - based days.
var pattern = /[a-z0-9]+/i; // define case-insensitive regexp pattern.test('hello world'); // true 'abc123'.match(/[0-9]/g); // ['1', '2', '3']
alert('hello world!'); try { throw new Error('forbidden'); } catch (e) { if (e instanceof SomeException) { // yeah this is kind of dumb throw 403; } else if (e.name === 'AnotherError') { throw {description: 'something happened'} } } finally { throw 'FatalError' }
var obj = { name: 'Jozko', surname: 'Mrkvicka', wholeName: function(){ return this.name + ' ' + this.surname; } }
function Person(name, surname){ this.name = name; this.surname = surname; this.wholeName = function(){ return this.name + ' ' + this.surname; } } // don't forget new keyword! var jozko = new Person('Jozko', 'Mrkvicka');
var obj = Object.create(Object.prototype, {prop: {value: 'asdf'}})
var Person = Ember.Object.extend({ name: null, surname: null, wholeName: function(){ return this.get('name') + ' ' + this.get('surname'); }.property('name', 'surname'); }); var jozko = Person.create({name: 'Jozko', surname: 'Mrkvicka'});
var obj = { a: 'aa' } obj.b = 'bbb' Object.defineProperty(obj, 'c', { value: 'ccc', enumerable: 'false', configurable: 'false', }); obj.hasOwnProperty('c') == true;
var arr = [1,2,,,{a:'b'},]; arr.push('hello world'); arr.length === 6 // true arr.someProperty = 'value'; // define property on array var anotherArr = new Array(10); // define sparse array
typeof [1,2,3] === 'object' // WTF?
var a = function sth(){}
function sth(){}
var someFunction = function(){ alert(helloWorld()); // this works fine // this would fail // alert(helloWorldVariable()); // helloWorld function get hoisted to the top, so it can be called from everywhere // helloWorldVariable get hoisted but it assignment don't var helloWorldVariable = function helloWorld(){ return 'hello world'; } // closure return function(){ return a++; } }
They differ in invocation context (this)
alert('hello world')
list.sum()
var date = new Date
list.sum.apply([1,2])
(function(){ var obj={}, privateVariable = 1; obj.publicFunction = function(){ return privateVariable; } return obj; }());
var fn = function(){ var a = 1; return function(){ return a++; } } clos = fn(); clos(); // 1 clos(); // 2 clos(); // 3 ...
Object has prototypes. If property is not found in current object, search throught prototype chain for it.
// this is set to new object function Rabbit(name, type) { this.name = name; // 'private' encapsulated property this.type = function(){ return type; } } Rabbit.prototype = new Animal; // define functions in prototype Rabbit.prototype.jump = function() { this.canWalk = true; } Rabbit.LACO = new Rabbit('Laco'); // don't forget new keyword var rabbit = new Rabbit('John')
var obj = Object.create(Object.prototype, {x: {value: 5}})
// obj.prototype is Object.prototype var obj = {a:5}
// the ES5 way object.isPrototypeOf(anotherObj) // the old way object instanceof SomeConstructor
perform conversions when necessary
serves as breakpoint if debugger is running
ES5 directive for strict code - indicates that strict code follows
evaluate some string as javascript code - don't use it
eval('alert("hello world!");
temporarily extend scope chain - don't use it
var a = 'aa', obj = {x = 'xx'}; with(obj){ x === 'xx'; // search on object a === 'aa'; // search from 'outer' scope c = 'cc'; // yikes! global scope }
Window serves as global object which provides API and some useful objects(document,location...) and methods (alert, setTimeout,...)
async phase
there is no specification where should be content visible (browser specific)
window.onload
DOM manipulation
Cookies/localstorage,...
inspect network communication
Browse through js/css/html files
profile css/javascript code
using for logging, debugging, pause on exception