Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

# Saving this for later?

### Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

#### Text the download link to your phone

Standard text messaging rates apply

# Rubyslava2102

5,703
views

Published on

1 Comment
6 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
No Downloads
Views
Total Views
5,703
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
85
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. JavaScript Márius ŠajgalíkRubyslava 21.2.2013 Márius Šajgalík 1
• 2. Ako vytvoriť objektvar o1 = { };var o2 = new Object();var o3 = { write: function(a) { console.log(a); } };o3.write(1);Rubyslava 21.2.2013 Márius Šajgalík 2
• 3. Ako vytvoriť objektfunction O4(){ this.write = function(a) { console.log(a); };}var O5 = function() { this.write = function(a) { console.log(a); };};var o4 = new O4();o4.write(1);Rubyslava 21.2.2013 Márius Šajgalík 3
• 4. Ako vytvoriť objektfunction Robot(_params) { var rotor; // private this.name; // public function init(params) { ... // compute _rotor from params rotor = _rotor; } init(_params);}Rubyslava 21.2.2013 Márius Šajgalík 4
• 5. Lokálne premennéfunction F1() { var x = 2; // poznáme iba vnútri F1 this.write = function() { console.log(x); };}var f1 = new F1();f1.write();f1.set = function(x2) { x = x2; }; // nové globálne xf1.set(3);f1.write(); // lokálne x nezmenenéconsole.log(x); // globálne x = 3Rubyslava 21.2.2013 Márius Šajgalík 5
• 6. thisfunction Robot(params){ var name = params.name; this.getName = function() { return name; }; this.getState = function() { ... return weather; }; this.hasUmbrella = function() { var state = ??? // this.getState is undefined };}Rubyslava 21.2.2013 Márius Šajgalík 6
• 7. thisfunction Robot(params){ ... var This = this; ... this.hasUmbrella = function() { var state = This.getState(); if (state.rain) return true; else return false; };}Rubyslava 21.2.2013 Márius Šajgalík 7
• 8. Ako zmeniť this?function predstavSa(meno) { console.log(Ahoj, som + this + a moje meno je + meno);}predstavSa.call(robot, Wall-E);predstavSa.apply(robot, [Wall-E]);Array.prototype.slice.call(Wall-E, 2, 5)Rubyslava 21.2.2013 Márius Šajgalík 8
• 9. Slovníkvar slovnik = { };slovnik[apple] = jablko;slovnik["oclock"] = hodín;slovnik[apple]; // jablkoslovnik[jablko]; // undefinedslovnik[constructor]; // functionRubyslava 21.2.2013 Márius Šajgalík 9
• 10. Slovníkfunction _slovnik() { var slovnik = { }; this.get = function(word) { if (word == constructor) word = _ + word; return slovnik[word]; }; this.set = function(word, translation) { if (word == constructor) word = _ + word; slovnik[word] = translation; };}var slovnik = new _slovnik();Rubyslava 21.2.2013 Márius Šajgalík 10
• 11. Slovníkfunction _slovnik() { var slovnik = { }; this.get = function(word) { return slovnik[_ + word]; }; this.set = function(word, translation) { slovnik[_ + word] = translation; };}var slovnik = new _slovnik();Rubyslava 21.2.2013 Márius Šajgalík 11
• 12. Ako spraviť asynchrónne APIvar API = { };API.xhr = { get: function(params, callback) { ... } getJSON: function(params, callback) { ... } post: function(params, callback) { ... }};Rubyslava 21.2.2013 Márius Šajgalík 12
• 13. Ako spraviť asynchrónne APIvar API = { };API._db = function (database) { function init(params, callback) { ... }; this.get = function(params, callback) { ... }; this.set = function(params, callback) { ... }; init({ db_name: database }, function(response) { if (response.error) console.error(response.cause); });};API.db = new API._db(my database);Rubyslava 21.2.2013 Márius Šajgalík 13
• 14. Ako spraviť asynchrónne APIget: function(params, callback) { DB.get(params.table, params.key, function(value) { callback({ ok: true, value: value }); }, function(error) { callback({ error: true, cause: error }); });}Rubyslava 21.2.2013 Márius Šajgalík 14
• 15. Ako spraviť asynchrónne APIvar API = { };API.core = { getKeywords: function(params, callback) { ... }};Rubyslava 21.2.2013 Márius Šajgalík 15
• 16. Variabilné parametregetKeywords: function(params, callback) { if (!params.URL) params = { URL: params }; if (!params.html) { API.xhr.get(params.URL,function(response) { params.html = response.html; This.getKeywords(params, callback); }); return; }}Rubyslava 21.2.2013 Márius Šajgalík 16
• 17. Variabilné parametregetKeywords: function(params, callback) { ... if (!params.words) { params.words = tokenize(params.html); } ...}Rubyslava 21.2.2013 Márius Šajgalík 17
• 18. Variabilné parametregetKeywords: function(params, callback) { if (params.words) { getKeywordsFromWords(params, callback); return; } if (params.html) { getKeywordsFromHtml(params, callback); return; } ...}Rubyslava 21.2.2013 Márius Šajgalík 18
• 19. Kopírovanie objektov – dedenieinštancií?var O = function () { this.write = function (a) { console.log(a); };};var o = new O();o.write(1);var p = Object.create(o);p.write(1);o.write2 = function (a) { console.log(a + 1); };o.write2(1);p.write2 = function (a) { console.log(a - 1); };o.write2(1);p.write2(1);Rubyslava 21.2.2013 Márius Šajgalík 19
• 20. Ako písať zátvorky?function areWeFinished(){ ...}function areWeFinished() { ...}Rubyslava 21.2.2013 Márius Šajgalík 20
• 21. Ako písať zátvorky?function areWeFinished(){ return { yes: true };}Rubyslava 21.2.2013 Márius Šajgalík 21
• 22. Ako písať zátvorky?function areWeFinished(){ return ; // takto to interpretuje JavaScript { yes: true };}areWeFinished(); // == undefined !!!Rubyslava 21.2.2013 Márius Šajgalík 22
• 23. Ako písať zátvorky?function areWeFinished() { return { yes: true };}Rubyslava 21.2.2013 Márius Šajgalík 23
• 24. areWeFinished() == trueRubyslava 21.2.2013 Márius Šajgalík 24