/** * 1: ajax封装实现 1 */ function get(url, callback) { var http; http = new XMLHttpRequest(); http.open("GET",url, true); http.send(); http.onreadystatechange = function () { if(http.readyState == 4) { callback(JSON.parse(http.responseText)) } } } /** * 2: ajax封装实现 2 */ // (function (win) { // var $ = function () {}, // http = null; // // // 向方法 $ 中追加一个方法 init,用来初始化ajax请求 // $.init = function () { // http = new XMLHttpRequest(); // } // // // 向方法 $ 中追加一个方法 get,用来发送get请求 // $.get = function (url, callback) { // $.init() // http.open("GET",url, true); // http.send(); // http.onreadystatechange = function () { // if(http.readyState == 4) { // callback(http.responseText) // } // } // // } // win.$ = $; // })(window) /** * 2: ajax封装实现 2 精简版 */ // (win => { // var $ = () => {}, http = null; // // // 向方法 $ 中追加一个方法 init,用来初始化ajax请求 // $.init = () => http = new XMLHttpRequest(); // // // 向方法 $ 中追加一个方法 get,用来发送get请求 // $.get = (url, callback) => { // $.init() // http.open("GET",url, true); // http.send(); // http.onreadystatechange = () => http.readyState == 4 ? callback(http.responseText) : callback('noData') // } // win.$ = $; // })(window) // function f(a,b) { // var result = a + 100; // // b(result) // } // // function f1() { // f(1,function (data) { // console.log(data) // } ) // } // //