Last active
September 15, 2016 06:29
-
-
Save kirilkirkov/c43a5bdccd9a5c9124c77560ee2ffd04 to your computer and use it in GitHub Desktop.
THIS in javascript, promises, deferred, soap, restful, async-sync load..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this: | |
Когато се извиква от функция | |
myFunction = function () { | |
console.log(this); | |
}; | |
myFunction(); | |
в този случей ще върне главния window обект | |
Когато се извиква вътре в обект е референция към съмия него. | |
var myObject = { | |
someMethod: function () { | |
console.log(this.asd); | |
}, | |
asd: 'Test' | |
}; | |
myObject.someMethod(); | |
promise / deffered: | |
е конструктор който се използва за изпълнение на асинхронни таскове (такива които не зависят от зареждането на други неща), | |
при изпълнение връща два аргумента resolve и reject съоветно когато задачата е изпълнена или ако има грешка, често се използва за ajax заявки. | |
в jquery когато при ajax използвам .done(function( data ) {... .fail(function ... дефакто идват от този интерфейс от зад.. | |
Пример: | |
if (window.Promise) { | |
console.log('Promise found'); | |
var promise = new Promise(function(resolve, reject) { | |
var request = new XMLHttpRequest(); | |
request.open('GET', 'http://api.icndb.com/jokes/random'); | |
request.onload = function() { | |
if (request.status == 200) { | |
resolve(request.response); // we got data here, so resolve the Promise | |
} else { | |
reject(Error(request.statusText)); // status is not 200 OK, so reject | |
} | |
}; | |
request.onerror = function() { | |
reject(Error('Error fetching data.')); // error occurred, reject the Promise | |
}; | |
request.send(); //send the request | |
}); | |
console.log('Asynchronous request made.'); | |
promise.then(function(data) { | |
console.log('Got data! Promise fulfilled.'); | |
document.getElementsByTagName('body')[0].textContent = JSON.parse(data).value.joke; | |
}, function(error) { | |
console.log('Promise rejected.'); | |
console.log(error.message); | |
}); | |
} else { | |
console.log('Promise not available'); | |
} | |
restful: | |
Начин/архитектура за трансфер на данни в web, предаване на данни чрез json, xml, text | |
soap: | |
Web сервиз, протокол ,информацията която предава е само xml |
Asynchronous technologies in js:
-
setInterval
-
setTimeout
-
requestAnimationFrame
-
XMLHttpRequest
-
WebSocket
-
Worker
-
Some HTML5 APIs such as the File API, Web Database API
-
Technologies that support onload
-
... many others
https://www.youtube.com/watch?v=_4KIm_zwN1I - Видео от Телерик
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Пример за синхронно и асинхронно зареждане на js. т.е. Функция след функция... или.. всички едновременно ;)